Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b2bc838

Browse files
committed
Java: add alert-suppression query for @SuppressWarnings("lgtm[...]")
1 parent 176d767 commit b2bc838

5 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* @name Alert suppression using annotations
3+
* @description Generates information about alert suppressions
4+
* using 'SuppressWarnings' annotations.
5+
* @kind alert-suppression
6+
* @id java/alert-suppression-annotations
7+
*/
8+
9+
import java
10+
import Metrics.Internal.Extents
11+
12+
/**
13+
* An alert suppression annotation.
14+
*/
15+
class SuppressionAnnotation extends SuppressWarningsAnnotation {
16+
string annotation;
17+
18+
SuppressionAnnotation() {
19+
exists(string text | text = this.getASuppressedWarningLiteral().getValue() |
20+
// match `lgtm[...]` anywhere in the comment
21+
annotation = text.regexpFind("(?i)\\blgtm\\s*\\[[^\\]]*\\]", _, _)
22+
)
23+
}
24+
25+
/**
26+
* Gets the text of this suppression annotation.
27+
*/
28+
string getText() { result = getASuppressedWarningLiteral().getValue() }
29+
30+
/** Gets the LGTM suppression annotation in this Java annotation. */
31+
string getAnnotation() { result = annotation }
32+
33+
/**
34+
* Holds if this annotation applies to the range from column `startcolumn` of line `startline`
35+
* to column `endcolumn` of line `endline` in file `filepath`.
36+
*/
37+
predicate covers(string filepath, int startline, int startcolumn, int endline, int endcolumn) {
38+
getAnnotatedElement().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
39+
}
40+
41+
/** Gets the scope of this suppression. */
42+
SuppressionScope getScope() { this = result.getSuppressionAnnotation() }
43+
}
44+
45+
/**
46+
* The scope of an alert suppression annotation.
47+
*/
48+
class SuppressionScope extends @annotation {
49+
SuppressionScope() { this instanceof SuppressionAnnotation }
50+
51+
/** Gets a suppression annotation with this scope. */
52+
SuppressionAnnotation getSuppressionAnnotation() { result = this }
53+
54+
/**
55+
* Holds if this element is at the specified location.
56+
* The location spans column `startcolumn` of line `startline` to
57+
* column `endcolumn` of line `endline` in file `filepath`.
58+
* For more information, see
59+
* [Locations](https://help.semmle.com/QL/learn-ql/ql/locations.html).
60+
*/
61+
predicate hasLocationInfo(
62+
string filepath, int startline, int startcolumn, int endline, int endcolumn
63+
) {
64+
this.(SuppressionAnnotation).covers(filepath, startline, startcolumn, endline, endcolumn)
65+
}
66+
67+
/** Gets a textual representation of this element. */
68+
string toString() { result = "suppression range" }
69+
}
70+
71+
from SuppressionAnnotation c
72+
select c, // suppression comment
73+
c.getText(), // text of suppression comment (excluding delimiters)
74+
c.getAnnotation(), // text of suppression annotation
75+
c.getScope() // scope of suppression

java/ql/src/semmle/code/java/JDKAnnotations.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ class OverrideAnnotation extends Annotation {
1818
class SuppressWarningsAnnotation extends Annotation {
1919
SuppressWarningsAnnotation() { this.getType().hasQualifiedName("java.lang", "SuppressWarnings") }
2020

21+
/** Gets the `StringLiteral` of a warning suppressed by this annotation. */
22+
StringLiteral getASuppressedWarningLiteral() {
23+
result = this.getAValue() or
24+
result = this.getAValue().(ArrayInit).getAnInit()
25+
}
26+
2127
/** Gets the name of a warning suppressed by this annotation. */
2228
string getASuppressedWarning() {
2329
result = this.getAValue().(StringLiteral).getLiteral() or
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| TestSuppressWarnings.java:2:1:2:49 | SuppressWarnings | lgtm[java/non-sync-override] | lgtm[java/non-sync-override] | TestSuppressWarnings.java:4:7:17:5 | suppression range |
2+
| TestSuppressWarnings.java:5:5:5:31 | SuppressWarnings | lgtm[] | lgtm[] | TestSuppressWarnings.java:6:17:8:5 | suppression range |
3+
| TestSuppressWarnings.java:10:5:10:57 | SuppressWarnings | lgtm[java/confusing-method-name] | lgtm[java/confusing-method-name] | TestSuppressWarnings.java:11:17:13:5 | suppression range |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AlertSuppressionAnnotations.ql
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
@SuppressWarnings("lgtm[java/non-sync-override]")
3+
@Deprecated
4+
class TestSuppressWarnings {
5+
@SuppressWarnings("lgtm[]")
6+
public void test() {
7+
8+
}
9+
@Deprecated
10+
@SuppressWarnings("lgtm[java/confusing-method-name]")
11+
public void test2() {
12+
13+
}
14+
@SuppressWarnings("lgtm")
15+
public void test3() {
16+
17+
}
18+
}

0 commit comments

Comments
 (0)