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.

貢獻者指南