dotnet/roslyn

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

Aperta

#45.232 aperta il 16 giu 2020

 (5 commenti) (0 reazioni) (0 assegnatari)C# (4257 fork)batch import
Area-IDEConcept-Continuous Improvementhelp wanted

Metriche repository

Star
 (20.414 stelle)
Metriche merge PR
 (Merge medio 6g 17h) (256 PR mergiate in 30 g)

Descrizione

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.

Guida contributor