Skip to content

Commit

Permalink
Fix analyzer RCS1096 (#1558)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Oct 11, 2024
1 parent ca8d20f commit 05477ca
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<object> constantValue = semanticModel.GetConstantValue(right, cancellationToken);

if (!constantValue.HasValue)
return;

ulong value = SymbolUtility.GetEnumValueAsUInt64(constantValue.Value!, enumTypeSymbol);

if (FlagsUtility<ulong>.Instance.IsComposite(value))
return;
}
}
else if (!CSharpFactory.AreEquivalent(right, otherExpression))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}
}
}
");
}
}

0 comments on commit 05477ca

Please sign in to comment.