Skip to content

Commit

Permalink
Fix analyzer RCS1264 (#1604)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored Dec 29, 2024
1 parent 498bb29 commit f214e3e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
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

0 comments on commit f214e3e

Please sign in to comment.