diff --git a/ChangeLog.md b/ChangeLog.md index 79cc37da6b..84f47bbdf0 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix analyzer [RCS0053](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0053) ([PR](https://github.com/dotnet/roslynator/pull/1547)) - Fix analyzer [RCS1223](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1223) ([PR](https://github.com/dotnet/roslynator/pull/1552)) - Fix analyzer [RCS1140](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1140) ([PR](https://github.com/dotnet/roslynator/pull/1554)) +- Fix analyzer [RCS1096](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1096) ([PR](https://github.com/dotnet/roslynator/pull/1558)) - [CLI] Improve removing of unused symbols ([PR](https://github.com/dotnet/roslynator/pull/1550)) ## [4.12.7] - 2024-10-01 diff --git a/src/Analyzers/CSharp/Analysis/UseHasFlagMethodOrBitwiseOperatorAnalyzer.cs b/src/Analyzers/CSharp/Analysis/UseHasFlagMethodOrBitwiseOperatorAnalyzer.cs index 8743ae6f47..8f5c5886f0 100644 --- a/src/Analyzers/CSharp/Analysis/UseHasFlagMethodOrBitwiseOperatorAnalyzer.cs +++ b/src/Analyzers/CSharp/Analysis/UseHasFlagMethodOrBitwiseOperatorAnalyzer.cs @@ -79,8 +79,20 @@ private static void AnalyzeBitwiseAndExpression(SyntaxNodeAnalysisContext contex if (otherExpression.IsNumericLiteralExpression("0")) { - if (SyntaxUtility.IsCompositeEnumValue(right, semanticModel, cancellationToken)) - return; + var enumTypeSymbol = (INamedTypeSymbol)semanticModel.GetTypeSymbol(right, cancellationToken); + + if (enumTypeSymbol?.EnumUnderlyingType is not null) + { + Optional constantValue = semanticModel.GetConstantValue(right, cancellationToken); + + if (!constantValue.HasValue) + return; + + ulong value = SymbolUtility.GetEnumValueAsUInt64(constantValue.Value!, enumTypeSymbol); + + if (FlagsUtility.Instance.IsComposite(value)) + return; + } } else if (!CSharpFactory.AreEquivalent(right, otherExpression)) { diff --git a/src/Tests/Analyzers.Tests/RCS1096UseHasFlagMethodOrBitwiseOperatorTests.cs b/src/Tests/Analyzers.Tests/RCS1096UseHasFlagMethodOrBitwiseOperatorTests.cs index 8c8c75cc07..0f6a3bacdc 100644 --- a/src/Tests/Analyzers.Tests/RCS1096UseHasFlagMethodOrBitwiseOperatorTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1096UseHasFlagMethodOrBitwiseOperatorTests.cs @@ -267,6 +267,28 @@ bool M(int value) return (value & (value - 1)) == 0; } } +"); + } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseHasFlagMethodOrBitwiseOperator)] + public async Task TestNoDiagnostic_NotConstant() + { + await VerifyNoDiagnosticAsync(@" +using System.IO; + +class P +{ + private FileAttributes _flags = FileAttributes.Device | FileAttributes.Offline; + + void M() + { + var flag = FileAttributes.Device; + + if ((flag & _flags) != 0) + { + } + } +} "); } }