Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -385,5 +385,22 @@ public void NotAvailableInGeneratedCode6()
{
VerifyNotAvailableInGeneratedCode("TestDocument.AssemblyAttributes.cs");
}

[Test]
public void NotAvailableAutoGeneratedCode()
{
const string code = @"
// <auto-generated>
class C
{
public bool MyProperty { get; private set; }
public C(bool f)
{
MyProperty = f;
}
}";

NoDiagnostic(code, DiagnosticIds.UseGetterOnlyAutoProperty);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,21 @@ public void NotAvailableInGeneratedCode6()
{
VerifyNotAvailableInGeneratedCode("TestDocument.AssemblyAttributes.cs");
}

[Test]
public void NotAvailableAutoGeneratedCode()
{
const string code = @"
// <auto-generated>
class C
{
int Property
{
get { return 42; }
}
}";

NoDiagnostic(code, DiagnosticIds.UseExpressionBodiedMember);
}
}
}
17 changes: 17 additions & 0 deletions Source/CSharpEssentials.Tests/UseNameOf/UseNameOfAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,22 @@ public void NotAvailableInGeneratedCode6()
{
VerifyNotAvailableInGeneratedCode("TestDocument.AssemblyAttributes.cs");
}

[Test]
public void NotAvailableAutoGeneratedCode()
{
const string code = @"
// <auto-generated>
using System;
class C
{
void M(int x)
{
throw new ArgumentNullException(""x"");
}
}";

NoDiagnostic(code, DiagnosticIds.UseNameOf);
}
}
}
65 changes: 59 additions & 6 deletions Source/CSharpEssentials/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Immutable;
using System.IO;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

namespace CSharpEssentials
{
Expand Down Expand Up @@ -58,12 +58,12 @@ public static ArgumentInfo GetArgumentInfo(this SemanticModel semanticModel, Arg
{
if (semanticModel == null)
{
throw new ArgumentNullException("semanticModel");
throw new ArgumentNullException(nameof(semanticModel));
}

if (argument == null)
{
throw new ArgumentNullException("argument");
throw new ArgumentNullException(nameof(argument));
}

var argumentList = argument.Parent as ArgumentListSyntax;
Expand Down Expand Up @@ -136,7 +136,7 @@ public static ArgumentInfo GetArgumentInfo(this SemanticModel semanticModel, Arg
return default(ArgumentInfo);
}

private static bool IsGeneratedCode(string filePath)
private static bool IsGeneratedCodeFile(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
Expand Down Expand Up @@ -168,9 +168,62 @@ private static bool IsGeneratedCode(string filePath)
return false;
}

public static bool IsGeneratedCode(this SyntaxTree tree)
private static bool BeginsWithAutoGeneratedComment(SyntaxTree tree, CancellationToken cancellationToken)
{
return IsGeneratedCode(tree.FilePath);
var root = tree.GetRoot(cancellationToken);
if (root.Kind() == SyntaxKind.CompilationUnit &&
root.HasLeadingTrivia)
{
var leadingTrivia = root.GetLeadingTrivia();

foreach (var trivia in leadingTrivia)
{
if (trivia.Kind() != SyntaxKind.SingleLineCommentTrivia)
{
continue;
}

var text = trivia.ToString();

// Should start with single-line comment slashes. If not, move along.
if (text.Length < 2 || text[0] != '/' || text[1] != '/')
{
continue;
}

// Scan past whitespace.
int index = 2;
while (index < text.Length && char.IsWhiteSpace(text[index]))
{
index++;
}

// Check to see if the text of the comment starts with "<auto-generated>".
const string AutoGenerated = "<auto-generated>";

if (string.Compare(text, index, AutoGenerated, 0, AutoGenerated.Length, StringComparison.OrdinalIgnoreCase) == 0)
{
return true;
}
}
}

return false;
}

public static bool IsGeneratedCode(this SyntaxTree tree, CancellationToken cancellationToken)
{
if (IsGeneratedCodeFile(tree.FilePath))
{
return true;
}

if (BeginsWithAutoGeneratedComment(tree, cancellationToken))
{
return true;
}

return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private static Diagnostic CreateDiagnostic(ISymbol symbol, CancellationToken can
var property = (PropertyDeclarationSyntax)reference.GetSyntax(cancellationToken);

// If the property declaration is in generated code, we shouldn't create the diagnostic.
if (!property.SyntaxTree.IsGeneratedCode())
if (!property.SyntaxTree.IsGeneratedCode(cancellationToken))
{
var setter = property.AccessorList.Accessors.FirstOrDefault(a => a.IsKind(SyntaxKind.SetAccessorDeclaration));
if (setter != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override void Initialize(AnalysisContext context)

private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context)
{
if (context.Node.SyntaxTree.IsGeneratedCode())
if (context.Node.SyntaxTree.IsGeneratedCode(context.CancellationToken))
{
return;
}
Expand All @@ -49,7 +49,7 @@ private static void HandleMethodDeclaration(SyntaxNodeAnalysisContext context)

private void HandleOperatorDeclaration(SyntaxNodeAnalysisContext context)
{
if (context.Node.SyntaxTree.IsGeneratedCode())
if (context.Node.SyntaxTree.IsGeneratedCode(context.CancellationToken))
{
return;
}
Expand All @@ -70,7 +70,7 @@ private void HandleOperatorDeclaration(SyntaxNodeAnalysisContext context)

private void HandleConversionOperatorDeclaration(SyntaxNodeAnalysisContext context)
{
if (context.Node.SyntaxTree.IsGeneratedCode())
if (context.Node.SyntaxTree.IsGeneratedCode(context.CancellationToken))
{
return;
}
Expand All @@ -91,7 +91,7 @@ private void HandleConversionOperatorDeclaration(SyntaxNodeAnalysisContext conte

private static void HandlePropertyDeclaration(SyntaxNodeAnalysisContext context)
{
if (context.Node.SyntaxTree.IsGeneratedCode())
if (context.Node.SyntaxTree.IsGeneratedCode(context.CancellationToken))
{
return;
}
Expand All @@ -112,7 +112,7 @@ private static void HandlePropertyDeclaration(SyntaxNodeAnalysisContext context)

private void HandleIndexerDeclaration(SyntaxNodeAnalysisContext context)
{
if (context.Node.SyntaxTree.IsGeneratedCode())
if (context.Node.SyntaxTree.IsGeneratedCode(context.CancellationToken))
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/CSharpEssentials/UseNameOf/UseNameOfAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override void Initialize(AnalysisContext context)

private static void AnalyzeArgument(SyntaxNodeAnalysisContext context)
{
if (context.Node.SyntaxTree.IsGeneratedCode())
if (context.Node.SyntaxTree.IsGeneratedCode(context.CancellationToken))
{
return;
}
Expand Down