Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RCS1187 #1150

Merged
merged 7 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)).
jamesHargreaves12 marked this conversation as resolved.
Show resolved Hide resolved

## [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;
josefpihrt marked this conversation as resolved.
Show resolved Hide resolved
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 (IdentifierNameSyntax identifierName in value.DescendantNodesAndSelf().OfType<IdentifierNameSyntax>())
jamesHargreaves12 marked this conversation as resolved.
Show resolved Hide resolved
{
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;
}
");
}
}