bkoelman / ResharperCodeContractNullability

Reports diagnostics, helping you to annotate your source tree with (Item)NotNull / (Item)CanBeNull attributes.
https://www.jetbrains.com/resharper/help/Code_Analysis__Code_Annotations.html
Apache License 2.0
26 stars 4 forks source link

Support for local functions #32

Closed bkoelman closed 7 years ago

bkoelman commented 7 years ago

Local functions cannot have attributes on their return value, which leaves only parameters applicable for annotation. Resharper 2016.3.1 uses them, so they should be reported by our analyzer.

In the current release, parameters on local functions cause a NullReferenceException because symbol lookup fails (bug submitted). The easy fix would be adding a null check in the conversion from Syntax to Symbol, which just disables the exception.

Despite an incorrect warning, we could switch to using RegisterSymbolAction for parameters (works only in VS 2017 and up). But as it turns out, analyzers do not get called for local functions. Support for that is planned for a future release.

In VS 2017 RC, analyzers also do not get called for method symbols of local methods.

To work around all and get analysis on local function parameters, we should additionally try to use Syntax to get to local functions, then process its parameters and generate warnings for them. SyntaxKind.LocalFunctionStatement does not exist in earlier APIs, so we may need to do some reflection and conditional tests to maintain backwards compatibility.

bkoelman commented 7 years ago

Additionally, namespace import and simplification fails on parameters of local functions. See this bug.

bkoelman commented 7 years ago

Update: https://github.com/dotnet/roslyn/pull/16498 disallows attributes on parameters of local functions. Resharper was adapted accordingly.

bkoelman commented 7 years ago

Looks like we need to keep the context.Symbol null check in place: https://github.com/dotnet/roslyn/issues/16757#issuecomment-280799265