Skip to content

Commit

Permalink
feat: Detects reference parameters with null default values as nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
ironcev committed Jun 18, 2019
1 parent 00754fb commit 00a45be
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/Sharpen.Engine/Extensions/SyntaxNodeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,16 @@ public static IEnumerable<SyntaxNode> OfAnyOfKinds(this IEnumerable<SyntaxNode>
syntaxNode.IsKind(fifthKind) ||
syntaxNode.IsKind(sixthKind));
}

public static IEnumerable<SyntaxNode> OfAnyOfKinds(this IEnumerable<SyntaxNode> syntaxNodes, SyntaxKind firstKind, SyntaxKind secondKind, SyntaxKind thirdKind, SyntaxKind fourthKind, SyntaxKind fifthKind, SyntaxKind sixthKind, SyntaxKind seventhKind)
{
return syntaxNodes.Where(syntaxNode => syntaxNode.IsKind(firstKind) ||
syntaxNode.IsKind(secondKind) ||
syntaxNode.IsKind(thirdKind) ||
syntaxNode.IsKind(fourthKind) ||
syntaxNode.IsKind(fifthKind) ||
syntaxNode.IsKind(sixthKind) ||
syntaxNode.IsKind(seventhKind));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Sharpen.Engine.SharpenSuggestions.CSharp80.NullableReferenceTypes.Anal
// So far we will leave it like this with a workaround for filtering the duplicates.
// The current priority is to get v0.9.0 finally released.
// Topics to solve:
// - tree analyzers that have results other syntax trees
// - tree analyzers that have results in other syntax trees
// - multiple results (duplicates, same suggestion reported few times)
// - showing only results in the original scope not those out of it
// - how to treat the lined files that could appear in several project (at the moment one suggestion per project which makes sense)
Expand Down Expand Up @@ -50,7 +50,8 @@ public IEnumerable<AnalysisResult> Analyze(SyntaxTree syntaxTree, SemanticModel
SyntaxKind.NotEqualsExpression, // identifier != null
SyntaxKind.VariableDeclarator, // object _fieldIdentifier = null; object localVariableIdentifier = null;
SyntaxKind.ConditionalAccessExpression, // identifier?.Something;
SyntaxKind.CoalesceExpression // identifier ?? something;
SyntaxKind.CoalesceExpression, // identifier ?? something;
SyntaxKind.Parameter // object parameter = null;
)
.Select(GetNullableIdentifierSymbol)
.Where(symbol => symbol?.IsImplicitlyDeclared == false)
Expand Down Expand Up @@ -326,11 +327,16 @@ ISymbol GetNullableIdentifierSymbol(SyntaxNode node)
? semanticModel.GetSymbolInfo(conditionalAccess.Expression).Symbol
: null;

case ParameterSyntax parameter:
return IsSurelyNullable(parameter.Default?.Value)
? semanticModel.GetDeclaredSymbol(parameter)
: null;

default: return null;
}

// (BDW, "Surely" is a bit too strong word here since default expression will
// be surely evaluated to null only for reference types.)
// be _surely_ evaluated to null only for reference types.)
bool IsSurelyNullable(SyntaxNode potentiallyNullableNode)
{
// We do not do any data flow analysis here, of course :-)
Expand Down

0 comments on commit 00a45be

Please sign in to comment.