-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NoUsingDeclaration analyzer (#195)
- Fix the test framework so that test cases can import System.Net - Ignore SA1008 - Move StyleChecker.Refactoring.UnnecessaryUsing.Classes to StyleChecker.Refactoring - Add GetTypeInfoSupplier(SemanticModelAnalysisContext) methods to SmaContextExtentions class - Add GetFullNameWithoutNullability(ITypeSymbol) method to TypeSymbols class - Add ToSymbol(VariableDeclaratorSyntax) to ISymbolizer interface
- Loading branch information
1 parent
a888570
commit 6202f9b
Showing
17 changed files
with
1,142 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
StyleChecker/StyleChecker.Test/Refactoring/NoUsingDeclaration/AnalyzerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace StyleChecker.Test.Refactoring.NoUsingDeclaration; | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using StyleChecker.Refactoring.NoUsingDeclaration; | ||
using StyleChecker.Test.Framework; | ||
|
||
[TestClass] | ||
public sealed class AnalyzerTest : CodeFixVerifier | ||
{ | ||
public AnalyzerTest() | ||
: base(new Analyzer(), new CodeFixer()) | ||
{ | ||
} | ||
|
||
[TestMethod] | ||
public void Okay() | ||
=> VerifyDiagnostic(ReadText("Okay"), Atmosphere.Default); | ||
|
||
[TestMethod] | ||
public void Code() | ||
{ | ||
var code = ReadText("Code"); | ||
var fix = ReadText("CodeFix"); | ||
static Result Expected(Belief b) | ||
{ | ||
return b.ToResult( | ||
Analyzer.DiagnosticId, | ||
$"Insert 'using' before '{b.Message}'."); | ||
} | ||
|
||
VerifyDiagnosticAndFix(code, Atmosphere.Default, Expected, fix); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
StyleChecker/StyleChecker.Test/Refactoring/NoUsingDeclaration/Code.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#nullable enable | ||
namespace StyleChecker.Test.Refactoring.NoUsingDeclaration; | ||
|
||
using System; | ||
using System.IO; | ||
|
||
public sealed class Code | ||
{ | ||
public static void Main() | ||
{ | ||
var i = new StreamReader("file.txt"); | ||
//@ ^var | ||
StreamReader j = new("file.txt"); | ||
//@ ^StreamReader | ||
} | ||
|
||
public static void ExplicitTypeDeclarationsWithMultipleDeclarators() | ||
{ | ||
StreamReader i1 = new StreamReader("1.txt"), i2 = new StreamReader("2.txt"); | ||
//@ ^StreamReader | ||
StreamReader j1 = new StreamReader("1.txt"), j2 = new("2.txt"); | ||
//@ ^StreamReader | ||
StreamReader k1 = new("1.txt"), k2 = new StreamReader("2.txt"); | ||
//@ ^StreamReader | ||
} | ||
|
||
public static void Using() | ||
{ | ||
// StringReader 'i' does not need to be disposed | ||
StringReader i = new("hello"); | ||
// IDisposable 'j' should be disposed | ||
IDisposable j = new StringReader("world"); | ||
//@ ^IDisposable | ||
} | ||
|
||
public static void Nullable() | ||
{ | ||
StreamReader? k = new StreamReader("file.txt"); | ||
//@ ^StreamReader? | ||
} | ||
|
||
public static void KeepTrivia() | ||
{ | ||
/*foo*/ var i = new StreamReader("file.txt"); // bar | ||
//@ ^var | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
StyleChecker/StyleChecker.Test/Refactoring/NoUsingDeclaration/CodeFix.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#nullable enable | ||
namespace StyleChecker.Test.Refactoring.NoUsingDeclaration; | ||
|
||
using System; | ||
using System.IO; | ||
|
||
public sealed class Code | ||
{ | ||
public static void Main() | ||
{ | ||
using var i = new StreamReader("file.txt"); | ||
using StreamReader j = new("file.txt"); | ||
} | ||
|
||
public static void ExplicitTypeDeclarationsWithMultipleDeclarators() | ||
{ | ||
using StreamReader i1 = new StreamReader("1.txt"), i2 = new StreamReader("2.txt"); | ||
using StreamReader j1 = new StreamReader("1.txt"), j2 = new("2.txt"); | ||
using StreamReader k1 = new("1.txt"), k2 = new StreamReader("2.txt"); | ||
} | ||
|
||
public static void Using() | ||
{ | ||
// StringReader 'i' does not need to be disposed | ||
StringReader i = new("hello"); | ||
// IDisposable 'j' should be disposed | ||
using IDisposable j = new StringReader("world"); | ||
} | ||
|
||
public static void Nullable() | ||
{ | ||
using StreamReader? k = new StreamReader("file.txt"); | ||
} | ||
|
||
public static void KeepTrivia() | ||
{ | ||
/*foo*/ | ||
using var i = new StreamReader("file.txt"); // bar | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
StyleChecker/StyleChecker.Test/Refactoring/NoUsingDeclaration/Okay.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
#nullable enable | ||
namespace StyleChecker.Test.Refactoring.NoUsingDeclaration; | ||
|
||
using System; | ||
using System.IO; | ||
using System.Net.Sockets; | ||
|
||
public sealed class Okay | ||
{ | ||
private StreamReader? streamReader; | ||
|
||
private TextWriter? SharedWriter { get; set; } | ||
|
||
public void AssignedToFieldOrProperty() | ||
{ | ||
// Replacing 'var' with 'using var' makes no sense. | ||
var reader = new StreamReader("input.txt"); | ||
streamReader = reader; | ||
var writer = new StreamWriter("output.txt"); | ||
SharedWriter = writer; | ||
} | ||
|
||
public static BufferedStream UsedAsAParameter() | ||
{ | ||
// Replacing 'var' with 'using var' makes no sense. | ||
var clientSocket = new Socket( | ||
AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | ||
var b = clientSocket.Connected; | ||
var netStream = new NetworkStream(clientSocket, true); | ||
var bufStream = new BufferedStream(netStream, 1024); | ||
return bufStream; | ||
} | ||
|
||
// A factory method that returns a new IDisposable object. | ||
public static Socket? Returned(Uri uri, int port) | ||
{ | ||
// Replacing 'var' with 'using var' makes no sense. | ||
var socket = new Socket(SocketType.Stream, ProtocolType.Tcp); | ||
try | ||
{ | ||
socket.Connect(uri.Host, port); | ||
return socket; | ||
} | ||
catch (Exception) | ||
{ | ||
socket.Dispose(); | ||
} | ||
return null; | ||
} | ||
|
||
public static void Reassigned() | ||
{ | ||
// Replacing 'var' with 'using var' causes an error CS1656. | ||
var i = new StreamReader("file.txt"); | ||
Console.WriteLine(i.ReadLine()); | ||
i = new StreamReader("another.txt"); | ||
Console.WriteLine(i.ReadLine()); | ||
} | ||
|
||
public static Action Captured() | ||
{ | ||
var i = new StreamWriter("file.txt"); | ||
return () => | ||
{ | ||
i.WriteLine("hello"); | ||
}; | ||
} | ||
|
||
public static void Main() | ||
{ | ||
using var i = new StreamReader("file.txt"); | ||
// StringReader does not need to be disposed. | ||
var j = new StringReader("hello"); | ||
} | ||
|
||
public static void ExplicitTypeDeclarations() | ||
{ | ||
using StreamReader i = new("file.txt"); | ||
using IDisposable j = new StringReader("world"); | ||
} | ||
|
||
public static void Nullable() | ||
{ | ||
using StreamReader? k = new StreamReader("file.txt"); | ||
} | ||
|
||
public static void NotNewOperator() | ||
{ | ||
var out1 = Console.Out; | ||
TextWriter out2 = Console.Out; | ||
var file1 = NewStreamReader("file.txt"); | ||
StreamReader? file2 = NewStreamReader("file.txt"); | ||
} | ||
|
||
private static StreamReader? NewStreamReader(string path) | ||
{ | ||
try | ||
{ | ||
return new StreamReader(path); | ||
} | ||
catch (IOException) | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
private static void Loophole() | ||
{ | ||
var (r1, r2) = (new StreamReader("1"), new StreamReader("2")); | ||
TextReader[] array = [new StreamReader("1")]; | ||
var anotherArray = new[] { new StreamReader("1") }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...r/Refactoring/UnnecessaryUsing/Classes.cs → ...ecker/StyleChecker/Refactoring/Classes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.