Skip to content

Commit a893711

Browse files
committed
Fix compiler warnings
1 parent 5a7e399 commit a893711

11 files changed

Lines changed: 100 additions & 69 deletions

Roslyn.Testing/Analyzer/DiagnosticVerifier.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Roslyn.Testing.Analyzer;
1414

1515
internal static class DiagnosticAnalyzerTestExtensions
1616
{
17-
private static readonly MetadataReference CorlibReference =
17+
private static readonly MetadataReference CoreLibraryReference =
1818
MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
1919

2020
private static readonly MetadataReference SystemCoreReference =
@@ -26,7 +26,7 @@ internal static class DiagnosticAnalyzerTestExtensions
2626
private static readonly MetadataReference CodeAnalysisReference =
2727
MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location);
2828

29-
private static readonly MetadataReference SystemDiagReference =
29+
private static readonly MetadataReference SystemDiagnosticReference =
3030
MetadataReference.CreateFromFile(typeof(Process).Assembly.Location);
3131

3232
internal static string DefaultFilePathPrefix = "Test";
@@ -41,7 +41,7 @@ internal static class DiagnosticAnalyzerTestExtensions
4141

4242
/// <summary>
4343
/// Given classes in the form of strings, their language, and an
44-
/// IDiagnosticAnlayzer to apply to it, return the diagnostics found in the string
44+
/// IDiagnosticAnalyzer to apply to it, return the diagnostics found in the string
4545
/// after converting it to a document.
4646
/// </summary>
4747
/// <param name="sources"> Classes in the form of strings </param>
@@ -199,11 +199,11 @@ private static Project CreateProject(string[] sources, string language, IEnumera
199199
var solution = new AdhocWorkspace()
200200
.CurrentSolution
201201
.AddProject(projectId, TestProjectName, TestProjectName, language)
202-
.AddMetadataReference(projectId, CorlibReference)
202+
.AddMetadataReference(projectId, CoreLibraryReference)
203203
.AddMetadataReference(projectId, SystemCoreReference)
204204
.AddMetadataReference(projectId, CSharpSymbolsReference)
205205
.AddMetadataReference(projectId, CodeAnalysisReference)
206-
.AddMetadataReference(projectId, SystemDiagReference);
206+
.AddMetadataReference(projectId, SystemDiagnosticReference);
207207

208208
if (references != null)
209209
{

Roslyn.Testing/CodeFix/CodeFixProviderTestExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static VerifyCodeFixProviderResult VerifyFix(this CodeFixProvider codeFix
9090
for (var i = 0; i < attempts; ++i)
9191
{
9292
var actions = new List<CodeAction>();
93-
var context = new CodeFixContext(document, analyzerDiagnostics[0], (a, d) => actions.Add(a), CancellationToken.None);
93+
var context = new CodeFixContext(document, analyzerDiagnostics[0], (a, _) => actions.Add(a), CancellationToken.None);
9494

9595
codeFixProvider.RegisterCodeFixesAsync(context)
9696
.GetAwaiter()

System.IO.Abstractions.Analyzers/CodeActions/DirectoryInfoCodeAction.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
4242
var editor = await DocumentEditor.CreateAsync(_document, cancellationToken)
4343
.ConfigureAwait(false);
4444

45+
if (_creationExpressionSyntax.ArgumentList == null)
46+
{
47+
return await Formatter.FormatAsync(editor.GetChangedDocument(), cancellationToken: cancellationToken)
48+
.ConfigureAwait(false);
49+
}
50+
4551
var arguments = _creationExpressionSyntax.ArgumentList.Arguments.Select(x => x.ToFullString());
4652

4753
editor.ReplaceNode(_creationExpressionSyntax,

System.IO.Abstractions.Analyzers/CodeActions/FileServiceConstructorInitialCodeAction.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
7070

7171
if (systemIo == null)
7272
{
73-
editor.InsertBefore(compilationUnitSyntax.Usings.FirstOrDefault(),
74-
RoslynClassFileSystem.GetFileSystemUsing());
73+
if (compilationUnitSyntax.Usings.Any())
74+
{
75+
editor.InsertBefore(compilationUnitSyntax.Usings.FirstOrDefault()!,
76+
RoslynClassFileSystem.GetFileSystemUsing());
77+
}
7578
} else
7679
{
7780
editor.InsertAfter(systemIo, RoslynClassFileSystem.GetFileSystemUsing());

System.IO.Abstractions.Analyzers/CodeActions/FileServiceInterfaceInjectionCodeAction.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
7070

7171
if (systemIo == null)
7272
{
73-
editor.InsertBefore(compilationUnitSyntax.Usings.FirstOrDefault(),
74-
RoslynClassFileSystem.GetFileSystemUsing());
73+
if (compilationUnitSyntax.Usings.Any())
74+
{
75+
editor.InsertBefore(compilationUnitSyntax.Usings.FirstOrDefault()!,
76+
RoslynClassFileSystem.GetFileSystemUsing());
77+
}
7578
} else
7679
{
7780
editor.InsertAfter(systemIo, RoslynClassFileSystem.GetFileSystemUsing());

System.IO.Abstractions.Analyzers/CodeActions/FileStreamCodeAction.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
4242
var editor = await DocumentEditor.CreateAsync(_document, cancellationToken)
4343
.ConfigureAwait(false);
4444

45+
if (_creationExpressionSyntax.ArgumentList == null)
46+
{
47+
return await Formatter.FormatAsync(editor.GetChangedDocument(), cancellationToken: cancellationToken)
48+
.ConfigureAwait(false);
49+
}
50+
4551
var arguments = _creationExpressionSyntax.ArgumentList.Arguments.Select(x => x.ToFullString());
4652

4753
editor.ReplaceNode(_creationExpressionSyntax,

System.IO.Abstractions.Analyzers/CodeActions/FileSystemInvokeCodeAction.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
4141
var editor = await DocumentEditor.CreateAsync(_document, cancellationToken)
4242
.ConfigureAwait(false);
4343

44-
editor.ReplaceNode(_invocation,
45-
SF.ParseExpression(
46-
$"{_field.Declaration.Variables.FirstOrDefault().Identifier.Text}.{_invocation.NormalizeWhitespace().ToFullString()}"));
44+
if (_field.Declaration.Variables.Any())
45+
{
46+
editor.ReplaceNode(_invocation,
47+
SF.ParseExpression(
48+
$"{_field.Declaration.Variables.FirstOrDefault()?.Identifier.Text}.{_invocation.NormalizeWhitespace().ToFullString()}"));
49+
}
4750

4851
return await Formatter.FormatAsync(editor.GetChangedDocument(), cancellationToken: cancellationToken)
4952
.ConfigureAwait(false);

System.IO.Abstractions.Analyzers/CodeFixes/DirectoryInfoCodeFix.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,30 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
3838
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken)
3939
.ConfigureAwait(false);
4040

41-
var classDeclarationSyntax = root.FindNode(context.Span)
41+
var classDeclarationSyntax = root?.FindNode(context.Span)
4242
.FirstAncestorOrSelf<ClassDeclarationSyntax>();
4343

44-
if (RoslynClassFileSystem.HasFileSystemField(classDeclarationSyntax))
44+
if (classDeclarationSyntax is null || !RoslynClassFileSystem.HasFileSystemField(classDeclarationSyntax))
4545
{
46-
var creationExpressionSyntax = root.FindNode(context.Span)
47-
.FirstAncestorOrSelf<ObjectCreationExpressionSyntax>();
46+
return;
47+
}
4848

49-
var fieldDeclarationSyntax = classDeclarationSyntax
50-
.Members
51-
.OfType<FieldDeclarationSyntax>()
52-
.FirstOrDefault(x =>
53-
x.Declaration.Type.NormalizeWhitespace()
54-
.ToFullString()
55-
== RoslynClassFileSystem.GetFileSystemType()
56-
.ToFullString());
49+
var creationExpressionSyntax = root.FindNode(context.Span)
50+
.FirstAncestorOrSelf<ObjectCreationExpressionSyntax>();
5751

58-
context.RegisterCodeFix(new DirectoryInfoCodeAction(Title,
59-
context.Document,
60-
creationExpressionSyntax,
61-
fieldDeclarationSyntax),
62-
context.Diagnostics);
63-
}
52+
var fieldDeclarationSyntax = classDeclarationSyntax
53+
.Members
54+
.OfType<FieldDeclarationSyntax>()
55+
.FirstOrDefault(x =>
56+
x.Declaration.Type.NormalizeWhitespace()
57+
.ToFullString()
58+
== RoslynClassFileSystem.GetFileSystemType()
59+
.ToFullString());
60+
61+
context.RegisterCodeFix(new DirectoryInfoCodeAction(Title,
62+
context.Document,
63+
creationExpressionSyntax,
64+
fieldDeclarationSyntax),
65+
context.Diagnostics);
6466
}
6567
}

System.IO.Abstractions.Analyzers/CodeFixes/FileInfoCodeFix.cs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,30 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
3838
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken)
3939
.ConfigureAwait(false);
4040

41-
var classDeclarationSyntax = root.FindNode(context.Span)
41+
var classDeclarationSyntax = root?.FindNode(context.Span)
4242
.FirstAncestorOrSelf<ClassDeclarationSyntax>();
4343

44-
if (RoslynClassFileSystem.HasFileSystemField(classDeclarationSyntax))
44+
if (classDeclarationSyntax is null || !RoslynClassFileSystem.HasFileSystemField(classDeclarationSyntax))
4545
{
46-
var creationExpressionSyntax = root.FindNode(context.Span)
47-
.FirstAncestorOrSelf<ObjectCreationExpressionSyntax>();
46+
return;
47+
}
4848

49-
var fieldDeclarationSyntax = classDeclarationSyntax
50-
.Members
51-
.OfType<FieldDeclarationSyntax>()
52-
.FirstOrDefault(x =>
53-
x.Declaration.Type.NormalizeWhitespace()
54-
.ToFullString()
55-
== RoslynClassFileSystem.GetFileSystemType()
56-
.ToFullString());
49+
var creationExpressionSyntax = root.FindNode(context.Span)
50+
.FirstAncestorOrSelf<ObjectCreationExpressionSyntax>();
5751

58-
context.RegisterCodeFix(new FileInfoCodeAction(Title,
59-
context.Document,
60-
creationExpressionSyntax,
61-
fieldDeclarationSyntax),
62-
context.Diagnostics);
63-
}
52+
var fieldDeclarationSyntax = classDeclarationSyntax
53+
.Members
54+
.OfType<FieldDeclarationSyntax>()
55+
.FirstOrDefault(x =>
56+
x.Declaration.Type.NormalizeWhitespace()
57+
.ToFullString()
58+
== RoslynClassFileSystem.GetFileSystemType()
59+
.ToFullString());
60+
61+
context.RegisterCodeFix(new FileInfoCodeAction(Title,
62+
context.Document,
63+
creationExpressionSyntax,
64+
fieldDeclarationSyntax),
65+
context.Diagnostics);
6466
}
6567
}

System.IO.Abstractions.Analyzers/CodeFixes/FileServiceConstructorInitialCodeFix.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public override async sealed Task RegisterCodeFixesAsync(CodeFixContext context)
4242
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken)
4343
.ConfigureAwait(false);
4444

45+
if (root == null)
46+
{
47+
return;
48+
}
49+
4550
var classDeclarationSyntax = root.FindNode(context.Span)
4651
.FirstAncestorOrSelf<ClassDeclarationSyntax>();
4752

0 commit comments

Comments
 (0)