Skip to content

Commit

Permalink
Create rule S5797 (#4170)
Browse files Browse the repository at this point in the history
* Add php to rule S5797

* Create PHP rule S5797

* Update PHP rule S5797 examples

* Update rule S5797 includes

---------

Co-authored-by: rudy-regazzoni-sonarsource <[email protected]>
Co-authored-by: GabinL21 <[email protected]>
  • Loading branch information
3 people authored Sep 3, 2024
1 parent c6798c1 commit 5273746
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 46 deletions.
5 changes: 5 additions & 0 deletions rules/S5797/description.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This rule raises an issue when a constant expression is used as a condition in an ``++if++``, ``++elif++``, a conditional expression or other boolean expressions.

== Why is this an issue?

When a constant is used as a condition, either it has no effect on the execution flow and it can be removed, or some code will never be executed and it is a bug.
2 changes: 2 additions & 0 deletions rules/S5797/php/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
34 changes: 34 additions & 0 deletions rules/S5797/php/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
include::../description.adoc[]

== How to fix it

=== Code examples

==== Noncompliant code example

[source,php,diff-id=1,diff-type=noncompliant]
----
function foo() {
if (true) { // Noncompliant: the condition is always true
return 1;
} else {
return 2;
}
}
----

==== Compliant solution

[source,php,diff-id=1,diff-type=compliant]
----
function foo() {
$a = bar();
if ($a) {
return 1;
} else {
return 2;
}
}
----

include::../rule.adoc[]
43 changes: 31 additions & 12 deletions rules/S5797/python/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
include::../rule.adoc[]
include::../description.adoc[]

=== Code examples

==== Noncompliant code example

ifdef::env-github,rspecator-view[]
[source,python,diff-id=1,diff-type=noncompliant]
----
def foo():
a = True
if a: # Noncompliant: the condition is always true
return 1
else:
return 2
----

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

include::../message.adoc[]
==== Compliant solution

include::../highlighting.adoc[]
[source,python,diff-id=1,diff-type=compliant]
----
def foo():
a = bar()
if a:
return 1
else:
return 2
----

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

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

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

* Python documentation - https://www.python.org/dev/peps/pep-0285/[PEP 285 - Adding a bool type]
* Python documentation - https://docs.python.org/3/library/stdtypes.html#truth-value-testing[Python documentation - Truth Value Testing]

include::../rule.adoc[]
45 changes: 11 additions & 34 deletions rules/S5797/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
This rule raises an issue when a constant expression is used as a condition in an ``++if++``, ``++elif++``, a conditional expression or other boolean expressions.
ifdef::env-github,rspecator-view[]

== Why is this an issue?
'''
== Implementation Specification
(visible only on this page)

When a constant is used as a condition, either it has no effect on the execution flow and it can be removed, or some code will never be executed and it is a bug.
include::./message.adoc[]

include::./highlighting.adoc[]

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

[source,python,diff-id=1,diff-type=noncompliant]
----
def foo():
a = True
if a: # Noncompliant: the condition is always true
return 1
else:
return 2
----


=== Compliant solution

[source,python,diff-id=1,diff-type=compliant]
----
def foo():
a = bar()
if a:
return 1
else:
return 2
----


== Resources

=== Documentation

* Python documentation - https://www.python.org/dev/peps/pep-0285/[PEP 285 - Adding a bool type]
* Python documentation - https://docs.python.org/3/library/stdtypes.html#truth-value-testing[Python documentation - Truth Value Testing]
include::./comments-and-links.adoc[]

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

0 comments on commit 5273746

Please sign in to comment.