dotnet / dotnet/roslyn

Prefer using the is false pattern matching over inverting a boolean expression

Open
#80,579 0 comments 10 reactions 0 assignees View on GitHub
Area-IDE
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

**Brief description:**

There should be a settings to prefer using the `is false` pattern instead of inverting a boolean expression. This is because a lot of people find it very easy to accidentally miss the `!` in the 2nd approach when reading over the code, especially when the expression gets lengthy because of variable/class/method names.

**Languages applicable:**

Both I believe *(I work with C# primarily so not sure if it's applicable to VB syntax)*.

**Code example that the analyzer should report:**

```cs
// Prefer
if (foo is false) {
}

// Over
if (!foo) {
}
```

**Additional information:**

I have found the [existing pattern matching preferences](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/language-rules#pattern-matching-preferences) and there don't seem to be any boolean ones *([csharp_style_prefer_pattern_matching](https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0078-ide0260#csharp_style_prefer_pattern_matching-ide0078) does not seem to trigger for this code snippet, but I think that's okay and that a new rule should be added)*, I also think that a `is true` pattern preference should be added *(for consistency),* although I don't think it would be used as much.

**Documentation requirements:**

When this analyzer is implemented, it must be documented by following the steps at [Documentation for IDE CodeStyle analyzers](https://github.com/dotnet/roslyn/blob/main/docs/contributing/Documentation%20for%20IDE%20CodeStyle%20analyzers.md).

Contributor guide

Open the contributing guide

Research direction

Review the existing pattern-matching preferences and the behavior of csharp_style_prefer_pattern_matching (IDE0078/IDE0260) first. Then follow Documentation for IDE CodeStyle analyzers to determine the required documentation work; done means the new preference is implemented for the applicable languages, reports the shown inversion, and is documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, visualbasic
Domain
developer-experience, documentation, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.