dotnet/roslyn

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

Open

#45,232 opened on Jun 16, 2020

View on GitHub
 (5 comments) (0 reactions) (0 assignees)C# (4,257 forks)batch import
Area-IDEConcept-Continuous Improvementhelp wanted

Repository metrics

Stars
 (20,414 stars)
PR merge metrics
 (Avg merge 6d 17h) (256 merged PRs in 30d)

Description

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.

Contributor guide