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

Fix AD0001 in S138: NullReferenceException #7155

Merged
merged 1 commit into from
May 2, 2023
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 @@ -102,7 +102,8 @@ protected override string GetMethodKindAndName(SyntaxToken identifierToken)

private static IEnumerable<SyntaxToken> GetMethodTokens(LocalFunctionStatementSyntaxWrapper wrapper) =>
wrapper.ExpressionBody?.Expression.DescendantTokens()
?? wrapper.Body.Statements.SelectMany(s => s.DescendantTokens());
?? wrapper.Body?.Statements.SelectMany(s => s.DescendantTokens())
?? Enumerable.Empty<SyntaxToken>();

private static long CountLines(LocalFunctionStatementSyntaxWrapper wrapper) =>
GetMethodTokens(wrapper).SelectMany(x => x.GetLineNumbers())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public void MethodsShouldNotHaveTooManyLines_CustomValues_CS() =>
public void MethodsShouldNotHaveTooManyLines_LocalFunctions() =>
CreateCSBuilder(5).AddPaths("MethodsShouldNotHaveTooManyLines.LocalFunctions.cs").WithOptions(ParseOptionsHelper.FromCSharp8).Verify();

[TestMethod]
public void MethodsShouldNotHaveTooManyLines_LocalFunctions_CSharp9() =>
CreateCSBuilder(5).AddPaths("MethodsShouldNotHaveTooManyLines.LocalFunctions.CSharp9.cs").WithOptions(ParseOptionsHelper.FromCSharp9).Verify();

[TestMethod]
public void MethodsShouldNotHaveTooManyLines_CustomValues_CSharp9() =>
CreateCSBuilder(2).AddPaths("MethodsShouldNotHaveTooManyLines_CustomValues.CSharp9.cs").WithTopLevelStatements().Verify();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Runtime.InteropServices;

class WithExternLocalFunctions
{
void ShortWithSingleExtern() // Compliant, we do not count extern local functions
{
int i = 0;
i++;
i++;
i++;
i++;

[DllImport("libc")]
static extern int chmod(string pathname, int mode); // Compliant, we do not count lines of an externally defined function
}

void ShortWithMultipleExtern() // Compliant, we do not count extern local functions
{
int i = 0;
i++;
i++;

[DllImport("libc")] static extern int chmod1(string pathname, int mode); // Compliant
[DllImport("libc")] static extern int chmod2(string pathname, int mode); // Compliant
[DllImport("libc")] static extern int chmod3(string pathname, int mode); // Compliant
}

void Long() // Noncompliant {{This method 'Long' has 6 lines, which is greater than the 5 lines authorized. Split it into smaller methods.}}
{
int i = 0;
i++;
i++;
i++;
i++;
i++;

[DllImport("libc")]
static extern int chmod(string pathname, int mode); // Compliant, we do not count lines of an externally defined function
}
}