Skip to content

Commit

Permalink
chore: Update to C# 12.0 (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminMichaelis authored Jan 8, 2024
1 parent c3a9cf3 commit d8735b6
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<LangVersion>11.0</LangVersion>
<LangVersion>12.0</LangVersion>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ await ProcessProject(new FileInfo(Path.Combine("..", "..", "..", "..", "IntelliT

public static async Task ProcessProject(FileInfo projectFile)
{
if (projectFile is null)
{
throw new ArgumentNullException(nameof(projectFile));
}
ArgumentNullException.ThrowIfNull(projectFile);

using var workspace = MSBuildWorkspace.Create();
Project project = await workspace.OpenProjectAsync(projectFile.FullName).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public async void Sample() { }
Message = "Async methods should not return void",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 13, 31)
}
]
};

VerifyCSharpDiagnostic(test, expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,9 @@ private static DiagnosticResult GetExpectedDiagnosticResult(int line, int col)
Message = "Attributes should be on separate lines",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", line, col)
}
]
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ static void Main(string[] args)
Severity = DiagnosticSeverity.Warning,
Message = "Using the symbol 'DateTimeOffset.implicit operator DateTimeOffset(DateTime)' can result in unpredictable behavior",
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 10, 38)
}
]
});

}
Expand Down Expand Up @@ -73,9 +73,9 @@ static void Main(string[] args)
Severity = DiagnosticSeverity.Warning,
Message = "Using the symbol 'DateTimeOffset.implicit operator DateTimeOffset(DateTime)' can result in unpredictable behavior",
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 17, 17)
}
]
});

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ static void Main(string[] args)
Severity = DiagnosticSeverity.Info,
Message = "Favor using the method `EnumerateFiles` over the `GetFiles` method",
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 11, 30)
}
]
});
}

Expand Down Expand Up @@ -105,9 +105,9 @@ static void Main(string[] args)
Severity = DiagnosticSeverity.Info,
Message = "Favor using the method `EnumerateDirectories` over the `GetDirectories` method",
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 11, 30)
}
]
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ private static async Task<Document> ApplyFix(Document document, CodeAction codeA
/// <returns>A list of Diagnostics that only surfaced in the code after the CodeFix was applied</returns>
private static IEnumerable<Diagnostic> GetNewDiagnostics(IEnumerable<Diagnostic> diagnostics, IEnumerable<Diagnostic> newDiagnostics)
{
Diagnostic[] oldArray = diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray();
Diagnostic[] newArray = newDiagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray();
Diagnostic[] oldArray = [.. diagnostics.OrderBy(d => d.Location.SourceSpan.Start)];
Diagnostic[] newArray = [.. newDiagnostics.OrderBy(d => d.Location.SourceSpan.Start)];

int oldIndex = 0;
int newIndex = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public DiagnosticResultLocation[] Locations
{
get
{
_Locations ??= Array.Empty<DiagnosticResultLocation>();
_Locations ??= [];
return _Locations;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ private static Diagnostic[] GetSortedDiagnostics(string[] sources, string langua
/// <returns>An IEnumerable of Diagnostics that surfaced in the source code, sorted by Location</returns>
protected static Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyzer analyzer, Document[] documents)
{
if (documents is null)
{
throw new ArgumentNullException(nameof(documents));
}
ArgumentNullException.ThrowIfNull(documents);

var projects = new HashSet<Project>();
foreach (Document document in documents)
Expand Down Expand Up @@ -97,7 +94,7 @@ protected static Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyz
/// <returns>An IEnumerable containing the Diagnostics in order of Location</returns>
private static Diagnostic[] SortDiagnostics(IEnumerable<Diagnostic> diagnostics)
{
return diagnostics.OrderBy(d => d.Location.SourceSpan.Start).ToArray();
return [.. diagnostics.OrderBy(d => d.Location.SourceSpan.Start)];
}

#endregion
Expand Down Expand Up @@ -135,7 +132,7 @@ private static Document[] GetDocuments(string[] sources, string language)
/// <returns>A Document created from the source string</returns>
protected static Document CreateDocument(string source, string language = LanguageNames.CSharp)
{
return CreateProject(new[] { source }, language).Documents.First();
return CreateProject([source], language).Documents.First();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ class TypeName
Message = "Field 'MyField' should be named _PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 13, 27)
}
]
};

VerifyCSharpDiagnostic(test, expected);
Expand Down Expand Up @@ -111,9 +111,9 @@ class TypeName
Message = "Field '_myField' should be named _PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 13, 27)
}
]
};

VerifyCSharpDiagnostic(test, expected);
Expand Down Expand Up @@ -143,9 +143,9 @@ class TypeName
Message = "Field '__myField' should be named _PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 13, 27)
}
]
};

VerifyCSharpDiagnostic(test, expected);
Expand Down Expand Up @@ -443,9 +443,9 @@ class TypeName
Message = "Field '_My_Field' should be named _PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 13, 27)
}
]
};

VerifyCSharpDiagnostic(test, expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ string localMethod() {
Message = "Method 'localMethod' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 17, 24)
}
]
};

VerifyCSharpDiagnostic(test, expected);
Expand Down Expand Up @@ -152,9 +152,9 @@ public string myMethod()
Message = "Method 'myMethod' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 13, 27)
}
]
};

VerifyCSharpDiagnostic(test, expected);
Expand Down Expand Up @@ -187,9 +187,9 @@ public string _MyMethod()
Message = "Method '_MyMethod' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 13, 27)
}
]
};

VerifyCSharpDiagnostic(test, expected);
Expand Down Expand Up @@ -349,9 +349,9 @@ public string My_Method()
Message = "Method 'My_Method' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 13, 27)
}
]
};

VerifyCSharpDiagnostic(test, expected);
Expand Down Expand Up @@ -416,19 +416,19 @@ void IInterface.foo() { }
Message = "Method 'foo' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 13, 25)
}
]
};
var expected2 = new DiagnosticResult
{
Id = "INTL0003",
Message = "Method 'foo' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 18, 29)
}
]
};
VerifyCSharpDiagnostic(test, expected1, expected2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ class TypeName
Message = "Property 'myProperty' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 13, 27)
}
]
};

VerifyCSharpDiagnostic(test, expected);
Expand Down Expand Up @@ -111,9 +111,9 @@ class TypeName
Message = "Property '_MyProperty' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 13, 27)
}
]
};

VerifyCSharpDiagnostic(test, expected);
Expand Down Expand Up @@ -290,9 +290,9 @@ class TypeName
Message = "Property 'My_Property' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 13, 27)
}
]
};

VerifyCSharpDiagnostic(test, expected);
Expand Down Expand Up @@ -356,19 +356,19 @@ public class TypeName : IInterface
Message = "Property 'foo' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 13, 27)
}
]
};
var expected2 = new DiagnosticResult
{
Id = "INTL0002",
Message = "Property 'foo' should be PascalCase",
Severity = DiagnosticSeverity.Warning,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 18, 31)
}
]
};

VerifyCSharpDiagnostic(test, expected1, expected2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ public void Foo()
Id = Analyzers.UnusedLocalVariable.DiagnosticId,
Message = "Local variable 'foo' should be used",
Severity = DiagnosticSeverity.Info,
Locations = new[]
{
Locations =
[
new DiagnosticResultLocation("Test0.cs", 15, 20)
}
]
};

VerifyCSharpDiagnostic(test, expected);
Expand Down Expand Up @@ -242,9 +242,9 @@ bool Bar(Func<bool, bool> func)
Message = "Local variable 't' should be used",
Severity = DiagnosticSeverity.Info,
Locations =
new[] {
[
new DiagnosticResultLocation("Test0.cs", 10, 17)
}
]
};
VerifyCSharpDiagnostic(test, result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected async Task VerifyBasicFix(string oldSource, string newSource, int? cod
private static async Task VerifyFix(string language, DiagnosticAnalyzer analyzer, CodeFixProvider codeFixProvider, string oldSource, string newSource, int? codeFixIndex, bool allowNewCompilerDiagnostics)
{
Document document = CreateDocument(oldSource, language);
Diagnostic[] analyzerDiagnostics = GetSortedDiagnosticsFromDocuments(analyzer, new[] { document });
Diagnostic[] analyzerDiagnostics = GetSortedDiagnosticsFromDocuments(analyzer, [document]);
IEnumerable<Diagnostic> compilerDiagnostics = GetCompilerDiagnostics(document);
int attempts = analyzerDiagnostics.Length;

Expand All @@ -86,7 +86,7 @@ private static async Task VerifyFix(string language, DiagnosticAnalyzer analyzer
var context = new CodeFixContext(document, analyzerDiagnostics[0], (a, d) => actions.Add(a), CancellationToken.None);
codeFixProvider.RegisterCodeFixesAsync(context).Wait();

if (!actions.Any())
if (actions.Count == 0)
{
break;
}
Expand All @@ -98,7 +98,7 @@ private static async Task VerifyFix(string language, DiagnosticAnalyzer analyzer
}

document = await ApplyFix(document, actions.ElementAt(0));
analyzerDiagnostics = GetSortedDiagnosticsFromDocuments(analyzer, new[] { document });
analyzerDiagnostics = GetSortedDiagnosticsFromDocuments(analyzer, [document]);

IEnumerable<Diagnostic> newCompilerDiagnostics = GetNewDiagnostics(compilerDiagnostics, GetCompilerDiagnostics(document));

Expand All @@ -114,7 +114,7 @@ private static async Task VerifyFix(string language, DiagnosticAnalyzer analyzer
}

//check if there are analyzer diagnostics left after the code fix
if (!analyzerDiagnostics.Any())
if (analyzerDiagnostics.Length == 0)
{
break;
}
Expand Down
Loading

0 comments on commit d8735b6

Please sign in to comment.