From 52737469445a29b6bf6a26b6f4172aad17ad2a9b Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 3 Sep 2024 12:13:36 +0200 Subject: [PATCH] Create rule S5797 (#4170) * Add php to rule S5797 * Create PHP rule S5797 * Update PHP rule S5797 examples * Update rule S5797 includes --------- Co-authored-by: rudy-regazzoni-sonarsource Co-authored-by: GabinL21 --- rules/S5797/description.adoc | 5 ++++ rules/S5797/php/metadata.json | 2 ++ rules/S5797/php/rule.adoc | 34 ++++++++++++++++++++++++++ rules/S5797/python/rule.adoc | 43 +++++++++++++++++++++++---------- rules/S5797/rule.adoc | 45 +++++++++-------------------------- 5 files changed, 83 insertions(+), 46 deletions(-) create mode 100644 rules/S5797/description.adoc create mode 100644 rules/S5797/php/metadata.json create mode 100644 rules/S5797/php/rule.adoc diff --git a/rules/S5797/description.adoc b/rules/S5797/description.adoc new file mode 100644 index 00000000000..09850b05f70 --- /dev/null +++ b/rules/S5797/description.adoc @@ -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. diff --git a/rules/S5797/php/metadata.json b/rules/S5797/php/metadata.json new file mode 100644 index 00000000000..7a73a41bfdf --- /dev/null +++ b/rules/S5797/php/metadata.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/rules/S5797/php/rule.adoc b/rules/S5797/php/rule.adoc new file mode 100644 index 00000000000..c8211f6024e --- /dev/null +++ b/rules/S5797/php/rule.adoc @@ -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[] diff --git a/rules/S5797/python/rule.adoc b/rules/S5797/python/rule.adoc index c45be3615f0..5523a2715ea 100644 --- a/rules/S5797/python/rule.adoc +++ b/rules/S5797/python/rule.adoc @@ -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[] diff --git a/rules/S5797/rule.adoc b/rules/S5797/rule.adoc index 78f353d8d3b..3068e7fd2df 100644 --- a/rules/S5797/rule.adoc +++ b/rules/S5797/rule.adoc @@ -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[]