diff --git a/ChangeLog.md b/ChangeLog.md index 6171a20476..d6bc1fe02d 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -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 diff --git a/src/Tests/Analyzers.Tests/RCS1009UseExplicitTypeInsteadOfVarInForEachTests.cs b/src/Tests/Analyzers.Tests/RCS1009UseExplicitTypeInsteadOfVarInForEachTests.cs index baa2d97362..81f9594898 100644 --- a/src/Tests/Analyzers.Tests/RCS1009UseExplicitTypeInsteadOfVarInForEachTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1009UseExplicitTypeInsteadOfVarInForEachTests.cs @@ -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) + { + } + } +} +"); + } + } diff --git a/src/Workspaces.Common/CSharp/DocumentRefactorings.cs b/src/Workspaces.Common/CSharp/DocumentRefactorings.cs index fb47f904fc..6684d6a0d5 100644 --- a/src/Workspaces.Common/CSharp/DocumentRefactorings.cs +++ b/src/Workspaces.Common/CSharp/DocumentRefactorings.cs @@ -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; @@ -106,7 +110,6 @@ private static TupleExpressionSyntax CreateTupleExpression(ITypeSymbol typeSymbo return tupleExpression.WithArguments(newArguments); } - public static Task ChangeTypeToVarAsync( Document document, TypeSyntax type,