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
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- [CLI] Add support for GitLab analyzer reports ([PR](https://github.com/dotnet/roslynator/pull/1633))

### Fixed

- Fix analyzer [RCS1264](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1264) ([PR](https://github.com/dotnet/roslynator/pull/1666))

## [4.13.1] - 2025-02-23

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private static void AnalyzeVariableDeclaration(SyntaxNodeAnalysisContext context

if (style == TypeStyle.Implicit)
{
if (CSharpTypeAnalysis.IsExplicitThatCanBeImplicit(variableDeclaration, context.SemanticModel, TypeAppearance.NotObvious, context.CancellationToken))
if (CSharpTypeAnalysis.IsExplicitThatCanBeImplicit(variableDeclaration, context.SemanticModel, context.CancellationToken))
ReportExplicitToImplicit(context, variableDeclaration.Type);
}
else if (style == TypeStyle.Explicit)
Expand Down
10 changes: 6 additions & 4 deletions src/CSharp/CSharp/CSharpTypeAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,13 @@ public static bool IsExplicitThatCanBeImplicit(
return IsTypeObvious(expression, typeSymbol, includeNullability: true, semanticModel, cancellationToken);
case TypeAppearance.NotObvious:
return !IsTypeObvious(expression, typeSymbol, includeNullability: true, semanticModel, cancellationToken);
case TypeAppearance.None:
return GetEqualityComparer(includeNullability: true).Equals(
typeSymbol,
semanticModel.GetTypeSymbol(expression, cancellationToken));
default:
throw new InvalidOperationException($"Unknow enum value '{typeAppearance}'");
}

Debug.Assert(typeAppearance == TypeAppearance.None, typeAppearance.ToString());

return true;
}

public static bool IsTypeObvious(ExpressionSyntax expression, SemanticModel semanticModel, CancellationToken cancellationToken)
Expand Down
38 changes: 35 additions & 3 deletions src/Tests/Analyzers.Tests/RCS1264UseVarOrExplicitTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ object M()
", options: Options.AddConfigOption(ConfigOptionKeys.UseVar, ConfigOptionValues.UseVar_Always));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseVarOrExplicitType)]
public async Task Test_ObjectCreationExpression()
{
await VerifyDiagnosticAndFixAsync(@"
class C
{
void M()
{
[|string[]|] arr = new string[1];
}
}
", @"
class C
{
void M()
{
var arr = new string[1];
}
}
", options: Options.AddConfigOption(ConfigOptionKeys.UseVar, ConfigOptionValues.UseVar_Always));
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseVarOrExplicitType)]
public async Task Test_TupleExpression()
{
Expand Down Expand Up @@ -177,16 +199,26 @@ class C
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseVarOrExplicitType)]
public async Task TestNoDiagnostic_ParseMethod()
public async Task TestDiagnostic_ParseMethod()
{
await VerifyNoDiagnosticAsync(@"
await VerifyDiagnosticAndFixAsync(@"
using System;

class C
{
void M()
{
[|TimeSpan|] timeSpan = TimeSpan.Parse(null);
}
}
", @"
using System;

class C
{
void M()
{
TimeSpan timeSpan = TimeSpan.Parse(null);
var timeSpan = TimeSpan.Parse(null);
}
}
", options: Options.AddConfigOption(ConfigOptionKeys.UseVar, ConfigOptionValues.UseVar_Always));
Expand Down
Loading