Skip to content

Commit dfa69c7

Browse files
authored
Fix analyzer RCS1264 (#1666)
1 parent 70cb73f commit dfa69c7

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

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

14+
### Fixed
15+
16+
- Fix analyzer [RCS1264](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1264) ([PR](https://github.com/dotnet/roslynator/pull/1666))
17+
1418
## [4.13.1] - 2025-02-23
1519

1620
### Added

src/Analyzers/CSharp/Analysis/UseVarOrExplicitTypeAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private static void AnalyzeVariableDeclaration(SyntaxNodeAnalysisContext context
4343

4444
if (style == TypeStyle.Implicit)
4545
{
46-
if (CSharpTypeAnalysis.IsExplicitThatCanBeImplicit(variableDeclaration, context.SemanticModel, TypeAppearance.NotObvious, context.CancellationToken))
46+
if (CSharpTypeAnalysis.IsExplicitThatCanBeImplicit(variableDeclaration, context.SemanticModel, context.CancellationToken))
4747
ReportExplicitToImplicit(context, variableDeclaration.Type);
4848
}
4949
else if (style == TypeStyle.Explicit)

src/CSharp/CSharp/CSharpTypeAnalysis.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,13 @@ public static bool IsExplicitThatCanBeImplicit(
250250
return IsTypeObvious(expression, typeSymbol, includeNullability: true, semanticModel, cancellationToken);
251251
case TypeAppearance.NotObvious:
252252
return !IsTypeObvious(expression, typeSymbol, includeNullability: true, semanticModel, cancellationToken);
253+
case TypeAppearance.None:
254+
return GetEqualityComparer(includeNullability: true).Equals(
255+
typeSymbol,
256+
semanticModel.GetTypeSymbol(expression, cancellationToken));
257+
default:
258+
throw new InvalidOperationException($"Unknow enum value '{typeAppearance}'");
253259
}
254-
255-
Debug.Assert(typeAppearance == TypeAppearance.None, typeAppearance.ToString());
256-
257-
return true;
258260
}
259261

260262
public static bool IsTypeObvious(ExpressionSyntax expression, SemanticModel semanticModel, CancellationToken cancellationToken)

src/Tests/Analyzers.Tests/RCS1264UseVarOrExplicitTypeTests.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,28 @@ object M()
3838
", options: Options.AddConfigOption(ConfigOptionKeys.UseVar, ConfigOptionValues.UseVar_Always));
3939
}
4040

41+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseVarOrExplicitType)]
42+
public async Task Test_ObjectCreationExpression()
43+
{
44+
await VerifyDiagnosticAndFixAsync(@"
45+
class C
46+
{
47+
void M()
48+
{
49+
[|string[]|] arr = new string[1];
50+
}
51+
}
52+
", @"
53+
class C
54+
{
55+
void M()
56+
{
57+
var arr = new string[1];
58+
}
59+
}
60+
", options: Options.AddConfigOption(ConfigOptionKeys.UseVar, ConfigOptionValues.UseVar_Always));
61+
}
62+
4163
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseVarOrExplicitType)]
4264
public async Task Test_TupleExpression()
4365
{
@@ -177,16 +199,26 @@ class C
177199
}
178200

179201
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseVarOrExplicitType)]
180-
public async Task TestNoDiagnostic_ParseMethod()
202+
public async Task TestDiagnostic_ParseMethod()
181203
{
182-
await VerifyNoDiagnosticAsync(@"
204+
await VerifyDiagnosticAndFixAsync(@"
205+
using System;
206+
207+
class C
208+
{
209+
void M()
210+
{
211+
[|TimeSpan|] timeSpan = TimeSpan.Parse(null);
212+
}
213+
}
214+
", @"
183215
using System;
184216
185217
class C
186218
{
187219
void M()
188220
{
189-
TimeSpan timeSpan = TimeSpan.Parse(null);
221+
var timeSpan = TimeSpan.Parse(null);
190222
}
191223
}
192224
", options: Options.AddConfigOption(ConfigOptionKeys.UseVar, ConfigOptionValues.UseVar_Always));

0 commit comments

Comments
 (0)