Skip to content

Commit 0d2e7c3

Browse files
SONARPY-764 S5914 should not fail on empty assertion (#1181)
1 parent 1b60fa7 commit 0d2e7c3

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

python-checks/src/main/java/org/sonar/python/checks/tests/UnconditionalAssertionCheck.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
package org.sonar.python.checks.tests;
2121

2222
import java.util.List;
23+
import java.util.Optional;
2324
import java.util.Set;
25+
import java.util.function.Consumer;
2426
import org.sonar.check.Rule;
2527
import org.sonar.plugins.python.api.PythonSubscriptionCheck;
2628
import org.sonar.plugins.python.api.SubscriptionContext;
@@ -85,17 +87,21 @@ public void initialize(Context context) {
8587
List<Argument> arguments = call.arguments();
8688

8789
if (BOOLEAN_ASSERTIONS.contains(name)) {
88-
checkBooleanAssertion(ctx, TreeUtils.nthArgumentOrKeyword(0, "expr", arguments));
90+
checkAssertion(arg -> checkBooleanAssertion(ctx, arg), 0, "expr", arguments);
8991
} else if (NONE_ASSERTIONS.contains(name)) {
90-
checkNoneAssertion(ctx, call, TreeUtils.nthArgumentOrKeyword(0, "expr", arguments));
92+
checkAssertion(arg -> checkNoneAssertion(ctx, call, arg),0, "expr", arguments);
9193
} else if (IS_ASSERTIONS.contains(name)) {
9294
String message = "assertIs".equals(name) ? IS_MESSAGE : IS_NOT_MESSAGE;
93-
checkIsAssertion(ctx, call, TreeUtils.nthArgumentOrKeyword(0, "first", arguments), message);
94-
checkIsAssertion(ctx, call, TreeUtils.nthArgumentOrKeyword(1, "second", arguments), message);
95+
checkAssertion(arg -> checkIsAssertion(ctx, call, arg, message), 0, "first", arguments);
96+
checkAssertion(arg -> checkIsAssertion(ctx, call, arg, message),1, "second", arguments);
9597
}
9698
});
9799
}
98100

101+
private static void checkAssertion(Consumer<RegularArgument> checkConsumer, int argPosition, String keyword, List<Argument> arguments) {
102+
Optional.ofNullable(TreeUtils.nthArgumentOrKeyword(argPosition, keyword, arguments)).ifPresent(checkConsumer);
103+
}
104+
99105
/**
100106
* `assert False` or `assert 0` is often used to make a test fail.
101107
* Usually it is better to use another assertion or throw an AssertionException.

python-checks/src/test/resources/checks/tests/unconditionalAssertion.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ def test_constant_assert_true_with_variables_pointing_to_single_immutable_type_v
123123
an_int = 0 # Overwrite all previus values
124124
self.assertTrue(an_int) # Noncompliant
125125

126-
126+
def test_empty_assertion(self):
127+
self.assertTrue()
127128

128129
#
129130
# RSPEC_5727: Comparison to None should not be constant

0 commit comments

Comments
 (0)