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 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)
{
}
}
}
");
}
}