Skip to content

Commit

Permalink
fix and test
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesHargreaves12 committed Aug 6, 2023
1 parent 5ebe544 commit afe0345
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Append `?` to nullable reference types.
- Fix [RCS1179](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1179.md) ([#1129](https://github.com/JosefPihrt/Roslynator/pull/1129)).
- Fix [RCS0060](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS0060.md) ([#1139](https://github.com/JosefPihrt/Roslynator/pull/1139)).
- Fix [RCS1187](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1187.md) ([#1150](https://github.com/JosefPihrt/Roslynator/pull/1150)).

## [4.3.0] - 2023-04-24

Expand Down
13 changes: 13 additions & 0 deletions src/Common/CSharp/Analysis/UseConstantInsteadOfFieldAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -82,6 +83,18 @@ internal static class UseConstantInsteadOfFieldAnalysis

if (!semanticModel.HasConstantValue(value, cancellationToken))
return false;

// Changing a static field to a constant changes the meaning of that symbol in the decorators.
foreach (var identifierName in value.DescendantNodesAndSelf().OfType<IdentifierNameSyntax>())
{
if (identifierName.Identifier.ValueText != fieldSymbol.Name)
continue;

if (identifierName.Parent is MemberAccessExpressionSyntax memberAccessExpression && memberAccessExpression.Name == identifierName)
continue;

return false;
}
}

foreach (IMethodSymbol constructorSymbol in fieldSymbol.ContainingType.StaticConstructors)
Expand Down
13 changes: 13 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1187UseConstantInsteadOfFieldTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ static void M(in int value)
{
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseConstantInsteadOfField)]
public async Task TestNoDiagnostic_SelfReference()
{
await VerifyNoDiagnosticAsync(@"
using System;
class C
{
private static readonly Double Double = Double.Epsilon;
}
");
}
}

0 comments on commit afe0345

Please sign in to comment.