dotnet/roslyn

Suggestion to use `obj is SomeConst` instead of safe cast or direct Equals(object) call

开放

#45,232 创建于 2020年6月16日

 (5 条评论) (0 个反应) (0 位负责人)C# (4,257 个派生)batch import
Area-IDEConcept-Continuous Improvementhelp wanted

仓库指标

星标
 (20,414 个星标)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 256 个 PR)

描述

obj as string == "x" and "x".Equals(obj) would be replaced with obj is "x".

This could apply to all constant types, so obj as bool? == SomeBoolConstant and SomeBoolConstant.Equals(obj) would be replaced with obj is SomeBoolConstant.

When CS0252 "Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string'" is reported, this fix could offer to change obj == "x" to obj is "x".

Happy to implement.

贡献者指南