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

Create rule S2583: Add VB.NET #2857

Merged
merged 5 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion rules/S2583/csharp/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void Sample(bool b)
}

var c = "xxx";
var res = c ?? "value"; // Noncompliant: d is always not null, "value" is never used
var res = c ?? "value"; // Noncompliant: c is always not null, "value" is never used
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved
}
----

Expand Down
3 changes: 3 additions & 0 deletions rules/S2583/vbnet/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"quickfix": "targeted"
}
113 changes: 113 additions & 0 deletions rules/S2583/vbnet/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
== Why is this an issue?

include::../description.adoc[]

In the case below, the call of `Dispose()` never happens.

[source,vbnet]
----
Dim a = False

If a Then
Dispose() ' Never reached
End If
----

=== Exceptions

This rule will not raise an issue in either of these cases:

* When the condition is a single `const bool`
+
[source,vbnet]
----
Const debug = False
'...
If debug Then
' Print something
End If
----
* When the condition is the literal `true` or `false`.

In these cases, it is obvious the code is as intended.
Tim-Pohlmann marked this conversation as resolved.
Show resolved Hide resolved

== How to fix it

The conditions should be reviewed to decide whether:

* to update the condition or
* to remove the condition.

=== Code examples

==== Noncompliant code example

[source,vbnet,diff-id=1,diff-type=noncompliant]
----
Public Sub Sample(ByVal b As Boolean)
Dim a = False
If a Then ' Noncompliant: The true branch is never reached
DoSomething() ' Never reached
End If

If Not a OrElse b Then ' Noncompliant: "not a" is always "True" and the false branch is never reached
DoSomething()
Else
DoSomethingElse() ' Never reached
End If

Dim c = "xxx"
Dim res = If(c, "value") ' Noncompliant: d is always not Nothing, "value" is never used
End Sub
----

==== Compliant solution

[source,vbnet,diff-id=1,diff-type=compliant]
----
Public Sub Sample(ByVal b As Boolean)
Dim a = False
If Foo(a) Then ' Condition was updated
DoSomething()
End If

If b Then ' Parts of the condition were removed.
DoSomething()
Else
DoSomethingElse()
End If

Dim c = "xxx"
Dim res = c ' "value" was removed
End Sub
----

include::../see.adoc[]

=== Documentation

* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/operators-and-expressions/logical-and-bitwise-operators[Logical and Bitwise Operators in Visual Basic]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/null-conditional-operators[?. and ?() null-conditional operators (Visual Basic)]
* Microsoft Learn - * Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/null-conditional-operators[If operator called with two arguments]

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

* Change this condition so that it does not always evaluate to "[true|false]"; some subsequent code is never executed.
* Change this expression which always evaluates to "not null"; some subsequent code is never executed.


include::../highlighting.adoc[]

'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]