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 S2930 FN #6685

Merged
merged 3 commits into from
Jan 30, 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
14 changes: 10 additions & 4 deletions analyzers/src/SonarAnalyzer.CSharp/Rules/DisposableNotDisposed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,17 @@ private static void TrackAssignmentsToLocalsAndPrivateFields(INamedTypeSymbol na
private static bool IsNodeInsideUsingStatement(SyntaxNode node)
{
var ancestors = node.AncestorsAndSelf().ToArray();
var usingStatements = ancestors.OfType<UsingStatementSyntax>();
var usingDeclarations = ancestors.OfType<LocalDeclarationStatementSyntax>();

return usingStatements.Any(x => ancestors.Contains(x.Expression) || ancestors.Contains(x.Declaration))
|| usingDeclarations.Any(x => ancestors.Contains(x.Declaration));
var isPartOfUsingStatement = ancestors
.OfType<UsingStatementSyntax>()
.Any(x => (x.Expression is not null && x.Expression.DescendantNodesAndSelf().Contains(node))
|| (x.Declaration is not null && x.Declaration.DescendantNodesAndSelf().Contains(node)));

var isPartOfUsingDeclaration = ancestors
.OfType<LocalDeclarationStatementSyntax>()
.Any(x => x.UsingKeyword() != default);

return isPartOfUsingStatement || isPartOfUsingDeclaration;
}

private static IEnumerable<SyntaxNode> GetDescendantNodes(INamedTypeSymbol namedType, SyntaxNode typeDeclaration) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ public async Task DisposeAsynchronously()
FileStream fs5;
await using ((fs5 = new FileStream(@"c:\foo.txt", FileMode.Open)).ConfigureAwait(false))
{
// do nothing
var fs5_1 = new FileStream(@"c:\foo.txt", FileMode.Open); // Noncompliant
fs5_1 = new FileStream(@"c:\foo.txt", FileMode.Open); // Noncompliant

using (var fs5_2 = new FileStream(@"c:\foo.txt", FileMode.Open))
{
fs5_1 = new FileStream(@"c:\foo.txt", FileMode.Open); // Noncompliant
}
}

FileStream fs6;
Expand Down