From c156995c78126b3be890a6fd0c4e10d8553e07eb Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Mon, 28 Jun 2021 08:43:05 +0200 Subject: [PATCH 1/3] Add imports instead of fully qualifying --- .../UseImplicitOrExplicitTypeCodeFix.cs | 3 +- .../UseImplicitOrExplicitTypeAnalyzerTests.cs | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/CSharp/YAnalyzers.CSharp.CodeFixes/UseImplicitOrExplicitTypeCodeFix.cs b/src/CSharp/YAnalyzers.CSharp.CodeFixes/UseImplicitOrExplicitTypeCodeFix.cs index 35463ef..6319c70 100644 --- a/src/CSharp/YAnalyzers.CSharp.CodeFixes/UseImplicitOrExplicitTypeCodeFix.cs +++ b/src/CSharp/YAnalyzers.CSharp.CodeFixes/UseImplicitOrExplicitTypeCodeFix.cs @@ -11,6 +11,7 @@ using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Editing; +using Microsoft.CodeAnalysis.Simplification; namespace YAnalyzers.CSharp { @@ -90,7 +91,7 @@ private static async Task UseExplicitTypeAsync(Document document, Synt TypeInfo typeInfo = model.GetTypeInfo(typeSyntax, cancellationToken); SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document); - SyntaxNode newNode = generator.TypeExpression(typeInfo.ConvertedType).WithTriviaFrom(typeSyntax); + SyntaxNode newNode = generator.TypeExpression(typeInfo.ConvertedType).WithTriviaFrom(typeSyntax).WithAdditionalAnnotations(Simplifier.AddImportsAnnotation); return document.WithSyntaxRoot(root.ReplaceNode(typeSyntax, newNode)); } diff --git a/src/UnitTests/UseImplicitOrExplicitTypeAnalyzerTests.cs b/src/UnitTests/UseImplicitOrExplicitTypeAnalyzerTests.cs index 52ae80d..2f773c3 100644 --- a/src/UnitTests/UseImplicitOrExplicitTypeAnalyzerTests.cs +++ b/src/UnitTests/UseImplicitOrExplicitTypeAnalyzerTests.cs @@ -449,6 +449,35 @@ public void Process(string s) } } } +"; + await VerifyCS.VerifyCodeFixAsync(code, VerifyCS.Diagnostic(UseImplicitOrExplicitTypeAnalyzer.UseExplicitTypeDiagnosticId).WithLocation(0), fixedCode); + } + + [TestMethod] + public async Task TestAddUsing() + { + var code = @" +using System.Linq; + +class C +{ + public void M(string[] s) + { + {|#0:var x = s.ToList()|}; + } +} +"; + var fixedCode = @" +using System.Collections.Generic; +using System.Linq; + +class C +{ + public void M(string[] s) + { + List x = s.ToList(); + } +} "; await VerifyCS.VerifyCodeFixAsync(code, VerifyCS.Diagnostic(UseImplicitOrExplicitTypeAnalyzer.UseExplicitTypeDiagnosticId).WithLocation(0), fixedCode); } From 85f9b2ba33c3fcf9c1d660ae5f62b9729b1f30d3 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Mon, 28 Jun 2021 08:58:03 +0200 Subject: [PATCH 2/3] Add skipped test --- .../UseImplicitOrExplicitTypeAnalyzerTests.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/UnitTests/UseImplicitOrExplicitTypeAnalyzerTests.cs b/src/UnitTests/UseImplicitOrExplicitTypeAnalyzerTests.cs index 2f773c3..9aa3d0e 100644 --- a/src/UnitTests/UseImplicitOrExplicitTypeAnalyzerTests.cs +++ b/src/UnitTests/UseImplicitOrExplicitTypeAnalyzerTests.cs @@ -478,6 +478,43 @@ public void M(string[] s) List x = s.ToList(); } } +"; + await VerifyCS.VerifyCodeFixAsync(code, VerifyCS.Diagnostic(UseImplicitOrExplicitTypeAnalyzer.UseExplicitTypeDiagnosticId).WithLocation(0), fixedCode); + } + + [TestMethod] + [Ignore("https://github.com/dotnet/roslyn/issues/54437 - https://github.com/Youssef1313/YAnalyzers/issues/33")] + public async Task TestDeconstruction() + { + var code = @" +using System.Collections.Generic; +using System.Linq; + +class C +{ + public void M(string[] s) + { + var x = new Dictionary(); + foreach ({|#0:var (a, b)|} in x) + { + } + } +} +"; + var fixedCode = @" +using System.Collections.Generic; +using System.Linq; + +class C +{ + public void M(string[] s) + { + var x = new Dictionary(); + foreach ((int a, string b) in x) + { + } + } +} "; await VerifyCS.VerifyCodeFixAsync(code, VerifyCS.Diagnostic(UseImplicitOrExplicitTypeAnalyzer.UseExplicitTypeDiagnosticId).WithLocation(0), fixedCode); } From 05a65f3888fa9cec9c903a2dc7cccd92d826bfa4 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Mon, 28 Jun 2021 09:20:25 +0200 Subject: [PATCH 3/3] Skip on non-Windows --- .../UseImplicitOrExplicitTypeAnalyzerTests.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/UnitTests/UseImplicitOrExplicitTypeAnalyzerTests.cs b/src/UnitTests/UseImplicitOrExplicitTypeAnalyzerTests.cs index 9aa3d0e..7fbe6a4 100644 --- a/src/UnitTests/UseImplicitOrExplicitTypeAnalyzerTests.cs +++ b/src/UnitTests/UseImplicitOrExplicitTypeAnalyzerTests.cs @@ -1,4 +1,6 @@ -using System.Threading.Tasks; +using System; +using System.Runtime.InteropServices; +using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; using VerifyCS = YAnalyzers.Test.CSharpCodeFixVerifier< @@ -456,6 +458,14 @@ public void Process(string s) [TestMethod] public async Task TestAddUsing() { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + // Ugly way to conditionally skip a test. + // This should be removed anyway. + // https://github.com/dotnet/roslyn-sdk/issues/876 + return; + } + var code = @" using System.Linq;