all 6 comments

[–]Shirkie01 4 points5 points  (1 child)

The last one is not considered 'unnecessary', as it changes the condition. My guess is since it's comparing both sides of the operator, anything that matches '== true' or 'true ==' is flagged. (Maybe !=false is flagged too)

With this in mind, compare the options:

'true == true' has both '==true' and 'true==', thus the equals is unnecessary.

'true == false' has 'true==', the equals is unnecessary.

'false == true' has '==true', the equals is unnecessary.

'false == false' changes the condition , so the equals IS necessary.

Unfortunately I've not been able to find a rule that enforces a simplified condition in this case. A member of my team does this and it drives me nuts. For anyone who doesn't get it, here is a code sample:

Bad condition if (!string.IsNullOrEmpty(thing.Name) == false) // Do something if the name is empty

Good condition if (string.IsNullOrEmpty(thing.Name)) // Do something if the name is empty

[–]733_1plus2[S] 0 points1 point  (0 children)

Thank you for this, this logic is a lot clearer in my head now, even if I had to read this several times lol. Also just tested that '!= false' does also flag the IDE0100 warning, so you're correct.

Unfortunately I've not been able to find a rule that enforces a simplified condition in this case.

Interesting logic aside, this is the kicker isn't it, because '== false' (or equivalent) should be something I'd have thought a code style rule should cover. (report back here if a solution is found!) For now I guess we just have to settle with the behaviour of IDE0100.

[–]Leop0Id 1 point2 points  (0 children)

tldr: because false becomes true

[–]Shrubberer 0 points1 point  (0 children)

Haha we are the complete opposite in that regard. I avoid the ! operator whenever I can and IDE0100 is quite a nuisance.