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 S3655 FN: VB implicit conversions #6991

Merged
merged 4 commits into from
Mar 28, 2023

Conversation

Tim-Pohlmann
Copy link
Contributor

Follow up to #6795

@Tim-Pohlmann Tim-Pohlmann added Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues. Sprint: SE labels Mar 27, 2023
@Tim-Pohlmann Tim-Pohlmann added this to the 8.56 milestone Mar 27, 2023
Copy link
Contributor

@antonioaversa antonioaversa left a comment

Choose a reason for hiding this comment

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

I think there is a problem on this extension of the ShouldExecute: we are now finding false positives every time a = Nothing is made.

When I paste the following example

Sub Assignment2(nullable As Integer?)
        If nullable = Nothing Then
            Dim x As Integer = nullable     ' Noncompliant
End If

into a VB.NET Project or LINQPad I get the following warning from Roslyn: Warning BC42037 This expression will always evaluate to Nothing (due to null propagation from the equals operator). To check if the value is null consider using 'Is Nothing'.

nullable = Nothing always evaluates to Nothing, which is implicitly converted to the Boolean value False. As a result of that, Dim x As Integer = nullable is never executed, and the non-compliance is in this case a false positive.

The same applies to all cases below. I think the right way of extending the rule was to capture Is Nothing instead, which is the VB.NET equivalent of == null.

Copy link
Contributor

@antonioaversa antonioaversa left a comment

Choose a reason for hiding this comment

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

LGTM

I would just expand tests to include <> Nothing.

@@ -584,6 +612,14 @@ Class WithAliases
End Sub
End Class

Class EqualsOperator
Sub EqualsNothing(nullable As Integer?)
If nullable = Nothing Then ' Always evaluates to Nothing (=> false) due to null propagation
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add a test with <> Nothing (which has a behavior very similar to = Nothing:

Function ReturnStatement(nullable As Integer?) As Integer
    If nullable <> Nothing Then
        Return nullable ' Noncompliant FP
    End If
End Function

And also the following case, which shows how = and <> break tautology w.r.t. OrElse:

Function ReturnStatement(nullable As Integer?) As Integer
    If (nullable <> Nothing) OrElse (nullable = Nothing) Then
        Return nullable ' Noncompliant
    End If
End Function

Of course you could "exploit" the fact that = and <> lift Nothing to get all sort of ternary logic behaviors. I would let you decide how deep you think it's worth exploring this path.

@Tim-Pohlmann Tim-Pohlmann force-pushed the Tim/S3655-VB-ExpandShouldExecute branch from ab5f3d3 to 20b2bb8 Compare March 28, 2023 10:57
Copy link
Contributor

@pavel-mikula-sonarsource pavel-mikula-sonarsource left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -584,6 +612,26 @@ Class WithAliases
End Sub
End Class

Class EqualsOperator
Sub EqualsNothing(nullable As Integer?)
If nullable = Nothing Then ' Always evaluates to Nothing (=> false) due to null propagation
Copy link
Contributor

Choose a reason for hiding this comment

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

I would repeat this unusual comment in the testcases below as well.

@sonarcloud
Copy link

sonarcloud bot commented Mar 28, 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 28, 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

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@Tim-Pohlmann Tim-Pohlmann merged commit fe635f4 into feature/SE Mar 28, 2023
@Tim-Pohlmann Tim-Pohlmann deleted the Tim/S3655-VB-ExpandShouldExecute branch March 28, 2023 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: CFG/SE CFG and SE related issues. Area: VB.NET VB.NET rules related issues.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants