Skip to content

Instantly share code, notes, and snippets.

@fekberg
Created September 17, 2012 19:34
Show Gist options
  • Select an option

  • Save fekberg/3739296 to your computer and use it in GitHub Desktop.

Select an option

Save fekberg/3739296 to your computer and use it in GitHub Desktop.
Listing 10.15
public IEnumerable<CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
{
if (node.GetType() != typeof(LocalDeclarationStatementSyntax)) return null;
var localDeclaration = (LocalDeclarationStatementSyntax)node;
var semanticModel = document.GetSemanticModel(cancellationToken);
var containingBlock = localDeclaration.FirstAncestorOrSelf<BlockSyntax>();
if (containingBlock == null) return null;
var dataFlowAnalysis = semanticModel.AnalyzeDataFlow(containingBlock);
var variable = localDeclaration.Declaration.Variables.First();
var symbol = semanticModel.GetDeclaredSymbol(variable);
if (dataFlowAnalysis.ReadInside.Contains(symbol)) return null;
return new[] { new CodeIssue(CodeIssueKind.Warning, localDeclaration.Span, string.Format("Variable {0} is declared but never used", variable.Identifier)) };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment