Skip to content

Commit bbdaf5e

Browse files
SONARPY-264 : Rule S1135 : Track uses of 'TODO' tags (#1088)
* SONARPY-264 : Rule S1135 : Track uses of 'TODO' tags * SONARPY-264: Update rule description
1 parent 2d5b56c commit bbdaf5e

File tree

8 files changed

+5237
-0
lines changed

8 files changed

+5237
-0
lines changed

its/ruling/src/test/resources/expected/python-S1135.json

Lines changed: 5096 additions & 0 deletions
Large diffs are not rendered by default.

python-checks/src/main/java/org/sonar/python/checks/CheckList.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ public static Iterable<Class> getChecks() {
225225
StringReplaceCheck.class,
226226
StrongCryptographicKeysCheck.class,
227227
TempFileCreationCheck.class,
228+
ToDoCommentCheck.class,
228229
TooManyLinesInFileCheck.class,
229230
TooManyParametersCheck.class,
230231
TooManyReturnsCheck.class,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* SonarQube Python Plugin
3+
* Copyright (C) 2011-2022 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.python.checks;
21+
22+
import java.util.regex.Pattern;
23+
import org.sonar.check.Rule;
24+
import org.sonar.plugins.python.api.PythonSubscriptionCheck;
25+
import org.sonar.plugins.python.api.SubscriptionCheck;
26+
import org.sonar.plugins.python.api.tree.Token;
27+
import org.sonar.plugins.python.api.tree.Tree;
28+
import org.sonar.plugins.python.api.tree.Trivia;
29+
30+
@Rule(key = "S1135")
31+
public class ToDoCommentCheck extends PythonSubscriptionCheck {
32+
33+
private static final String TODO_COMMENT_PATTERN = "^#[ ]*TODO.*";
34+
private static final String MESSAGE = "Complete the task associated to this \"TODO\" comment.";
35+
36+
@Override
37+
public void initialize(SubscriptionCheck.Context context) {
38+
Pattern pattern = Pattern.compile(TODO_COMMENT_PATTERN, Pattern.CASE_INSENSITIVE);
39+
context.registerSyntaxNodeConsumer(Tree.Kind.TOKEN, ctx -> {
40+
Token token = (Token) ctx.syntaxNode();
41+
for (Trivia trivia : token.trivia()) {
42+
String comment = trivia.value();
43+
if (pattern.matcher(comment).matches()) {
44+
ctx.addIssue(trivia.token(), MESSAGE);
45+
}
46+
}
47+
});
48+
}
49+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<p><code>TODO</code> tags are commonly used to mark places where some more code is required, but which the developer wants to implement later.</p>
2+
<p>Sometimes the developer will not have the time or will simply forget to get back to that tag.</p>
3+
<p>This rule is meant to track those tags and to ensure that they do not go unnoticed.</p>
4+
<h2>Noncompliant Code Example</h2>
5+
<pre>
6+
def doSomething:
7+
# TODO : Complete function
8+
</pre>
9+
<h2>See</h2>
10+
<ul>
11+
<li> <a href="https://cwe.mitre.org/data/definitions/546.html">MITRE, CWE-546</a> - Suspicious Comment </li>
12+
</ul>
13+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"title": "Track uses of \"TODO\" tags",
3+
"type": "CODE_SMELL",
4+
"status": "ready",
5+
"remediation": {
6+
"func": "Constant\/Issue",
7+
"constantCost": "0min"
8+
},
9+
"tags": [
10+
"cwe"
11+
],
12+
"defaultSeverity": "Info",
13+
"ruleSpecification": "RSPEC-1135",
14+
"sqKey": "S1135",
15+
"scope": "All",
16+
"securityStandards": {
17+
"CWE": [
18+
546
19+
]
20+
},
21+
"quickfix": "unknown"
22+
}

python-checks/src/main/resources/org/sonar/l10n/py/rules/python/Sonar_way_profile.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"S1066",
2121
"S1110",
2222
"S1134",
23+
"S1135",
2324
"S1143",
2425
"S1144",
2526
"S1186",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* SonarQube Python Plugin
3+
* Copyright (C) 2011-2022 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.python.checks;
21+
22+
import org.junit.Test;
23+
import org.sonar.python.checks.utils.PythonCheckVerifier;
24+
25+
public class ToDoCommentCheckTest {
26+
27+
@Test
28+
public void test() {
29+
PythonCheckVerifier.verify("src/test/resources/checks/todoComment.py", new ToDoCommentCheck());
30+
}
31+
32+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def divide(numerator, denominator):
2+
# Noncompliant@+1 {{Complete the task associated to this "TODO" comment.}}
3+
return numerator / denominator # TODO denominator value might be 0
4+
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
# Noncompliant@+1
6+
#TODO
7+
8+
# this is not TODO
9+
10+
# Noncompliant@+1
11+
# todo in lower case
12+
13+
for d in lib_dirs:
14+
# TODO: some TODO
15+
# Noncompliant@-1
16+
pass
17+
18+
if True:
19+
print("a")
20+
# Noncompliant@+1
21+
# TODO: something
22+
for d in lib_dirs:
23+
pass

0 commit comments

Comments
 (0)