Skip to content

Commit

Permalink
Fix RCS1009's handling of discard designations. (#1063)
Browse files Browse the repository at this point in the history
Co-authored-by: Josef Pihrt <josef.pihrt@outlook.com>
  • Loading branch information
jamesHargreaves12 and josefpihrt authored Apr 14, 2023
1 parent ffcec2a commit f3caaca
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Do not report `System.Windows.DependencyPropertyChangedEventArgs` as unused parameter ([RCS1163](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1163.md)) ([#1068](https://github.com/JosefPihrt/Roslynator/pull/1068)).
- Fix ([RCS1032](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1032.md)) ([#1064](https://github.com/JosefPihrt/Roslynator/pull/1064)).
- Update processing of .globalconfig file to prioritize file-specific diagnostic severities over global diagnostic severities. [#1066](https://github.com/JosefPihrt/Roslynator/pull/1066/files)
- Fix RCS1009 to handles discard designations ([#1063](https://github.com/JosefPihrt/Roslynator/pull/1063/files)).

## [4.2.0] - 2022-11-27

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,77 @@ public void M()
}
}");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseExplicitTypeInsteadOfVarWhenTypeIsObvious)]
public async Task Test_TupleExpression_WithDiscardPredefinedType()
{
await VerifyDiagnosticAndFixAsync(@"
using System;
using System.Collections.Generic;
class C
{
void M()
{
var items = new List<(object, System.DateTime)>();
foreach ([|var|] (_, y) in items)
{
}
}
}
", @"
using System;
using System.Collections.Generic;
class C
{
void M()
{
var items = new List<(object, System.DateTime)>();
foreach ((object _, DateTime y) in items)
{
}
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseExplicitTypeInsteadOfVarWhenTypeIsObvious)]
public async Task Test_TupleExpression_WithDiscardNonPredefinedType()
{
await VerifyDiagnosticAndFixAsync(@"
using System;
using System.Collections.Generic;
class C
{
void M()
{
var items = new List<(object, System.DateTime)>();
foreach ([|var|] (x, _) in items)
{
}
}
}
", @"
using System;
using System.Collections.Generic;
class C
{
void M()
{
var items = new List<(object, System.DateTime)>();
foreach ((object x, DateTime _) in items)
{
}
}
}
");
}

}
7 changes: 5 additions & 2 deletions src/Workspaces.Common/CSharp/DocumentRefactorings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ private static TupleExpressionSyntax CreateTupleExpression(ITypeSymbol typeSymbo
{
if (f.Expression is DeclarationExpressionSyntax declarationExpression)
return f.WithExpression(declarationExpression.WithType(declarationExpression.Type.WithSimplifierAnnotation()));
if (f.Expression is PredefinedTypeSyntax or MemberAccessExpressionSyntax)
{
return f.WithExpression(DeclarationExpression(ParseTypeName(f.Expression.ToString()).WithSimplifierAnnotation(), DiscardDesignation()));
}
SyntaxDebug.Fail(f.Expression);
return f;
Expand All @@ -106,7 +110,6 @@ private static TupleExpressionSyntax CreateTupleExpression(ITypeSymbol typeSymbo

return tupleExpression.WithArguments(newArguments);
}

public static Task<Document> ChangeTypeToVarAsync(
Document document,
TypeSyntax type,
Expand Down

0 comments on commit f3caaca

Please sign in to comment.