Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unused variable lambda #108

Merged
merged 4 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -140,10 +140,82 @@ public void Descriptor_ContainsExpectedValues()
Assert.AreEqual("Flow", diagnostic.Category);
Assert.AreEqual(DiagnosticSeverity.Info, diagnostic.DefaultSeverity);
Assert.AreEqual(true, diagnostic.IsEnabledByDefault);
Assert.AreEqual("All local variables should be accessed", diagnostic.Description);
Assert.AreEqual("All local variables should be accessed, or named with underscores to indicate they are unused", diagnostic.Description);
Assert.AreEqual("https://github.com/IntelliTect/CodingStandards", diagnostic.HelpLinkUri);
}

[TestMethod]
public void LambdaExpressionWithDiscard_NoDiagnosticInformationReturned()
{
string test = @"
using System;

namespace ConsoleApplication1
{
class TypeName
{
public void Foo()
{
Bar(_ => { return true; });
bool Bar(Func<bool, bool> func)
{
return func(true);
}
}
}
}";

VerifyCSharpDiagnostic(test);
}

[TestMethod]
public void LambdaExpressionWithTwoDiscards_NoDiagnosticInformationReturned()
{
string test = @"
using System;

namespace ConsoleApplication1
{
class TypeName
{
public void Foo()
{
Bar(__ => { return true; });
bool Bar(Func<bool, bool> func)
{
return func(true);
}
}
}
}";

VerifyCSharpDiagnostic(test);
}

[TestMethod]
public void LambdaMethodWithDiscard_NoDiagnosticInformationReturned()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the same as the first test? Should this be testing the opposite case confirming the un-used lambda parameters are flagged

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly wasn't sure, so figured I'd err on the side of caution. :) If having or not having braces is fundamentally the same, I can remove the test.

But, yes, I did forget to add tests for the opposite case. I will do that now!

{
string test = @"
using System;

namespace ConsoleApplication1
{
class TypeName
{
public void Foo()
{
Bar(_ => true);
bool Bar(Func<bool, bool> func)
{
return func(true);
}
}
}
}";

VerifyCSharpDiagnostic(test);
}

protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
{
return new Analyzers.UnusedLocalVariable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class UnusedLocalVariable : DiagnosticAnalyzer
public const string DiagnosticId = "INTL0303";
private const string _Title = "Local variable unused";
private const string _MessageFormat = "Local variable '{0}' should be used";
private const string _Description = "All local variables should be accessed";
private const string _Description = "All local variables should be accessed, or named with underscores to indicate they are unused";
private const string _Category = "Flow";
private const string _HelpLinkUri = "https://github.com/IntelliTect/CodingStandards";

Expand Down Expand Up @@ -43,7 +43,8 @@ private static void AnalyzeMethod(SyntaxNodeAnalysisContext context)

ImmutableArray<ISymbol> variablesDeclared = dataFlow.VariablesDeclared;
IEnumerable<ISymbol> variablesRead = dataFlow.ReadInside.Union(dataFlow.ReadOutside);
IEnumerable<ISymbol> unused = variablesDeclared.Except(variablesRead);
IEnumerable<ISymbol> unused = variablesDeclared.Except(variablesRead)
.Where(x => !(x.Name.All(c => c == '_')));

foreach (ISymbol unusedVar in unused)
{
Expand Down