Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ If you are already using other analyzers, you can check [which rules are duplica
|[MA0198](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0198.md)|Design|Specify cref for ambiguous inheritdoc on types|⚠️|✔️|✔️|
|[MA0199](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0199.md)|Design|Do not use inheritdoc on types without inheritance source|⚠️|✔️|❌|
|[MA0200](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0200.md)|Usage|Do not use empty property patterns with non-nullable value types|ℹ️|✔️|✔️|
|[MA0201](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0201.md)|Usage|Do not use zero-valued enum flags in flag checks|⚠️|✔️|❌|

<!-- rules -->

Expand Down
7 changes: 7 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
|[MA0198](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0198.md)|Design|Specify cref for ambiguous inheritdoc on types|<span title='Warning'>⚠️</span>|✔️|✔️|
|[MA0199](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0199.md)|Design|Do not use inheritdoc on types without inheritance source|<span title='Warning'>⚠️</span>|✔️|❌|
|[MA0200](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0200.md)|Usage|Do not use empty property patterns with non-nullable value types|<span title='Info'>ℹ️</span>|✔️|✔️|
|[MA0201](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0201.md)|Usage|Do not use zero-valued enum flags in flag checks|<span title='Warning'>⚠️</span>|✔️|❌|

|Id|Suppressed rule|Justification|
|--|---------------|-------------|
Expand Down Expand Up @@ -811,6 +812,9 @@ dotnet_diagnostic.MA0199.severity = warning

# MA0200: Do not use empty property patterns with non-nullable value types
dotnet_diagnostic.MA0200.severity = suggestion

# MA0201: Do not use zero-valued enum flags in flag checks
dotnet_diagnostic.MA0201.severity = warning
```

# .editorconfig - all rules disabled
Expand Down Expand Up @@ -1409,4 +1413,7 @@ dotnet_diagnostic.MA0199.severity = none

# MA0200: Do not use empty property patterns with non-nullable value types
dotnet_diagnostic.MA0200.severity = none

# MA0201: Do not use zero-valued enum flags in flag checks
dotnet_diagnostic.MA0201.severity = none
```
2 changes: 2 additions & 0 deletions docs/Rules/MA0192.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ This rule reports patterns such as:

For comparisons against `0`, the enum member used in the bitwise `&` must be a single-bit value (for example `1`, `2`, `4`, `8`, ...). Combined values are ignored.

Zero-valued enum members are not reported by this rule. They are covered by [MA0201](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0201.md).

## Non-compliant code

````csharp
Expand Down
46 changes: 46 additions & 0 deletions docs/Rules/MA0201.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# MA0201 - Do not use zero-valued enum flags in flag checks
<!-- sources -->
Source: [UseHasFlagMethodAnalyzer.cs](https://github.com/meziantou/Meziantou.Analyzer/blob/main/src/Meziantou.Analyzer/Rules/UseHasFlagMethodAnalyzer.cs)
<!-- sources -->

Using a zero-valued enum member (`None = 0`) as a flag to test always produces a constant result.

This rule reports patterns such as:

- `(value & MyEnum.None) == MyEnum.None` (always `true`)
- `(value & MyEnum.None) != MyEnum.None` (always `false`)
- `(value & MyEnum.None) is MyEnum.None` (always `true`)
- `(value & MyEnum.None) is not MyEnum.None` (always `false`)
- `value.HasFlag(MyEnum.None)` (always `true`)

## Non-compliant code

````csharp
[System.Flags]
enum MyEnum
{
None = 0,
Flag1 = 1,
}

bool M(MyEnum value)
{
return (value & MyEnum.None) == MyEnum.None;
}
````

## Compliant code

````csharp
[System.Flags]
enum MyEnum
{
None = 0,
Flag1 = 1,
}

bool M(MyEnum value)
{
return value.HasFlag(MyEnum.Flag1);
}
````
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,6 @@ dotnet_diagnostic.MA0199.severity = error

# MA0200: Do not use empty property patterns with non-nullable value types
dotnet_diagnostic.MA0200.severity = error

# MA0201: Do not use zero-valued enum flags in flag checks
dotnet_diagnostic.MA0201.severity = error
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,6 @@ dotnet_diagnostic.MA0199.severity = suggestion

# MA0200: Do not use empty property patterns with non-nullable value types
dotnet_diagnostic.MA0200.severity = suggestion

# MA0201: Do not use zero-valued enum flags in flag checks
dotnet_diagnostic.MA0201.severity = suggestion
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,6 @@ dotnet_diagnostic.MA0199.severity = warning

# MA0200: Do not use empty property patterns with non-nullable value types
dotnet_diagnostic.MA0200.severity = warning

# MA0201: Do not use zero-valued enum flags in flag checks
dotnet_diagnostic.MA0201.severity = warning
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,6 @@ dotnet_diagnostic.MA0199.severity = warning

# MA0200: Do not use empty property patterns with non-nullable value types
dotnet_diagnostic.MA0200.severity = suggestion

# MA0201: Do not use zero-valued enum flags in flag checks
dotnet_diagnostic.MA0201.severity = warning
3 changes: 3 additions & 0 deletions src/Meziantou.Analyzer.Pack/configuration/none.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,6 @@ dotnet_diagnostic.MA0199.severity = none

# MA0200: Do not use empty property patterns with non-nullable value types
dotnet_diagnostic.MA0200.severity = none

# MA0201: Do not use zero-valued enum flags in flag checks
dotnet_diagnostic.MA0201.severity = none
1 change: 1 addition & 0 deletions src/Meziantou.Analyzer/RuleIdentifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ internal static class RuleIdentifiers
public const string InheritdocShouldNotBeAmbiguousOnTypes = "MA0198";
public const string InheritdocShouldHaveSourceOnTypes = "MA0199";
public const string DoNotUseEmptyPropertyPatternOnNonNullableValueType = "MA0200";
public const string DoNotUseZeroValuedEnumFlagsInFlagChecks = "MA0201";

public static string GetHelpUri(string identifier)
{
Expand Down
Loading
Loading