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 analyzer RCS1264 #1604

Merged
merged 2 commits into from
Dec 29, 2024
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 @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fix refactoring 'Change accessibility' ([RR0186](https://josefpihrt.github.io/docs/roslynator/refactorings/RR0186)) ([PR](https://github.com/dotnet/roslynator/pull/1599))
- Fix analyzer [RCS1264](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1264) ([PR](https://github.com/dotnet/roslynator/pull/1604))

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private static void AnalyzeVariableDeclaration(SyntaxNodeAnalysisContext context
}
else if (style == TypeStyle.Explicit)
{
if (CSharpTypeAnalysis.IsImplicitThatCanBeExplicit(variableDeclaration, context.SemanticModel, TypeAppearance.Obvious, context.CancellationToken))
if (CSharpTypeAnalysis.IsImplicitThatCanBeExplicit(variableDeclaration, context.SemanticModel, context.CancellationToken))
ReportImplicitToExplicit(context, variableDeclaration.Type);
}
else if (style == TypeStyle.ImplicitWhenTypeIsObvious)
Expand Down
30 changes: 30 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1264UseVarOrExplicitTypeTests2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,36 @@ void M()
", options: Options.AddConfigOption(ConfigOptionKeys.UseVar, ConfigOptionValues.UseVar_Never));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseVarOrExplicitType)]
public async Task Test_NotObviousExpression()
{
await VerifyDiagnosticAndFixAsync(@"
using System.Threading.Tasks;
using System.Collections.Generic;

class C
{
void M()
{
[|var|] x1 = string.Empty;
[|var|] x2 = Task.FromResult(string.Empty);
}
}
", @"
using System.Threading.Tasks;
using System.Collections.Generic;

class C
{
void M()
{
string x1 = string.Empty;
Task<string> x2 = Task.FromResult(string.Empty);
}
}
", options: Options.AddConfigOption(ConfigOptionKeys.UseVar, ConfigOptionValues.UseVar_Never));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseVarOrExplicitType)]
public async Task TestNoDiagnostic_ForEach_DeclarationExpression()
{
Expand Down