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 RCS1009's handling of discard designations. #1063

Merged
merged 6 commits into from Apr 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog.md
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
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
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