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 S1168 FP: Does not respect nullable annotations #6952

Conversation

Corniel
Copy link
Contributor

@Corniel Corniel commented Mar 18, 2023

Fixes #6878
Fixes #6825
Add a check on the return type to check nullability.

@Corniel
Copy link
Contributor Author

Corniel commented Mar 18, 2023

I know the current solution is not something that should be shipped. In SonarAnalyzer.CFG the following code exists:

// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

namespace StyleCop.Analyzers.Lightup
{
    public readonly record struct NullabilityInfo
    {
       // ..
    }
}

Obviously that should become available in SonarAnalyzer.Common too. The question is how? Furthermore I noticed that in common there is the Helpers namespace with TypeHelper, and there is the Extensions namespace with NamedTypeInfoExtensions. Should that be updated?

@martin-strecker-sonarsource
Copy link
Contributor

see also #6878

@martin-strecker-sonarsource
Copy link
Contributor

see also #6825

@martin-strecker-sonarsource
Copy link
Contributor

Obviously that should become available in SonarAnalyzer.Common too.

We track the implementation for this in #6825 The idea is to do something

Should that be updated?

Yes. Something like in TypeInfoExtensions needs to be created:

public static class ITypeSymbolExtensions
{
    public NullableAnnotation NullableAnnotation(this ITypeSymbol symbol) { }
}

It is a bit easier as TypeInfoExtensions.Nullability(this TypeInfo typeInfo) but still needs the enum to enum conversion. Maybe one of the LightupHelpers.CreateSyntaxPropertyAccessor already does what we need (there is an overload that takes a type and calls Convert).

@Corniel
Copy link
Contributor Author

Corniel commented Mar 22, 2023

It is a bit easier as TypeInfoExtensions.Nullability(this TypeInfo typeInfo) but still needs the enum to enum conversion. Maybe one of the LightupHelpers.CreateSyntaxPropertyAccessor already does what we need (there is an overload that takes a type and calls Convert).

I could not find anything outside the CFG project. (I searched for NullableAnnotation). Do you know how in the past the LightupHelpers have been extended?

@pavel-mikula-sonarsource pavel-mikula-sonarsource changed the title FIX S1168: FP should not apply for explicitly nullable return types Fix S1168 FP: Does not respect nullable annotations Mar 22, 2023
@martin-strecker-sonarsource
Copy link
Contributor

You can have a look at this PR #6099. It shouldn't be as complicated in this case. A simple call to LightupHelpers.CreateSyntaxPropertyAccessor should return a proper Func if I'm not mistaken.

@Corniel Corniel force-pushed the corniel/s1168-not-on-nullable branch from 9e078e7 to 84d53c7 Compare March 23, 2023 19:56
@Corniel
Copy link
Contributor Author

Corniel commented Mar 23, 2023

@martin-strecker-sonarsource I've changed the implementation of the LightupHelpers.CreateSyntaxPropertyAccessor; when the return type of the function and property type don't match, I added a Expression.Convert() to the body. That should not break existing usage of this method, and makes our lives easier.

@Corniel Corniel force-pushed the corniel/s1168-not-on-nullable branch from 84d53c7 to e00084a Compare March 23, 2023 21:06
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice work, @Corniel! I just have some minor suggestions.

{
return FallbackAccessor;
}
if (!typeof(TProperty).GetTypeInfo().IsAssignableFrom(property.PropertyType.GetTypeInfo()))

Choose a reason for hiding this comment

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

👍That is exactly what we need here!

@martin-strecker-sonarsource
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

internal static class TypeHelper
{
#region TypeKind
private static readonly Func<ITypeSymbol, NullableAnnotation> NullableAnnotationAccessor = LightupHelpers.CreateSyntaxPropertyAccessor<ITypeSymbol, NullableAnnotation>(typeof(ITypeSymbol), nameof(NullableAnnotation));

Choose a reason for hiding this comment

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

This line is too long.

Corniel and others added 4 commits March 24, 2023 13:04
…InsteadOfNull.cs

Co-authored-by: Martin Strecker <103252490+martin-strecker-sonarsource@users.noreply.github.com>
Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM. @Corniel thank you for your contribution!

@martin-strecker-sonarsource
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@sonarcloud
Copy link

sonarcloud bot commented Mar 27, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@sonarcloud
Copy link

sonarcloud bot commented Mar 27, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

83.3% 83.3% Coverage
0.0% 0.0% Duplication

@Corniel
Copy link
Contributor Author

Corniel commented Mar 27, 2023

@martin-strecker-sonarsource Due to the updated indenting, some existing lines popped up that are not covered by existing unit tests. What to do?

  1. Remove lines
  2. Add tests
  3. Ignore coverage

@martin-strecker-sonarsource
Copy link
Contributor

  1. Ignore coverage

The coverage is only about the non-whitespace changes and this case: _ => null, We don't care about covering this case as it is just defensive coding.

@Corniel
Copy link
Contributor Author

Corniel commented Mar 27, 2023

  1. Ignore coverage

The coverage is only about the non-whitespace changes and this case: _ => null, We don't care about covering this case as it is just defensive coding.

I fully agree, but it is not my call, therefor the question.

@martin-strecker-sonarsource martin-strecker-sonarsource removed their assignment Mar 28, 2023
@pavel-mikula-sonarsource pavel-mikula-sonarsource merged commit e0d9b6e into SonarSource:master Mar 28, 2023
@Corniel Corniel deleted the corniel/s1168-not-on-nullable branch March 28, 2023 09:52
@martin-strecker-sonarsource
Copy link
Contributor

Peach validation:

  • aspnet-core: ~30 FPs fixed
  • dnspy: ~40FPs fixed
  • dnspy:extensions: ~120 FPs fixed
  • dotnet-runtim: ~140 FPs fixed
  • elasticsearch: 5 FPs fixed
  • embedio: 3 FPs fixed
  • hot-chocolate: 18 FPs fixed
  • nodatime: 2 FPs fixed
  • nunit: 4 FPs fixed

That's an impressive list @Corniel Thank you for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix S1168 FP: Does not respect nullable annotations Expand the ShimLayer with ITypeSymbol.NullableAnnotation
3 participants