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

Skip to content

Commit 62521dc

Browse files
committed
Java: account for multiple strings in java/alert-suppression-annotations
1 parent f3a980d commit 62521dc

3 files changed

Lines changed: 21 additions & 15 deletions

File tree

java/ql/src/AlertSuppressionAnnotations.ql

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,28 @@
99
import java
1010
import Metrics.Internal.Extents
1111

12+
/** Gets the LGTM suppression annotation text in the string `s`, if any. */
13+
bindingset[s]
14+
string getAnnotationText(string s) {
15+
// match `lgtm[...]` anywhere in the comment
16+
result = s.regexpFind("(?i)\\blgtm\\s*\\[[^\\]]*\\]", _, _)
17+
}
18+
1219
/**
1320
* An alert suppression annotation.
1421
*/
1522
class SuppressionAnnotation extends SuppressWarningsAnnotation {
16-
string annotation;
23+
string text;
1724

1825
SuppressionAnnotation() {
19-
exists(string text | text = this.getASuppressedWarningLiteral().getValue() |
20-
// match `lgtm[...]` anywhere in the comment
21-
annotation = text.regexpFind("(?i)\\blgtm\\s*\\[[^\\]]*\\]", _, _)
22-
)
26+
text = this.getASuppressedWarningLiteral().getValue() and
27+
exists(getAnnotationText(text))
2328
}
2429

2530
/**
2631
* Gets the text of this suppression annotation.
2732
*/
28-
string getText() { result = getASuppressedWarningLiteral().getValue() }
29-
30-
/** Gets the LGTM suppression annotation in this Java annotation. */
31-
string getAnnotationText() { result = annotation }
33+
string getText() { result = text }
3234

3335
private Annotation getASiblingAnnotation() {
3436
result = getAnnotatedElement().(Annotatable).getAnAnnotation() and
@@ -95,8 +97,11 @@ class SuppressionScope extends @annotation {
9597
string toString() { result = "suppression range" }
9698
}
9799

98-
from SuppressionAnnotation c
99-
select c, // suppression comment
100-
c.getText(), // text of suppression comment (excluding delimiters)
101-
c.getAnnotationText(), // text of suppression annotation
100+
from SuppressionAnnotation c, string text, string annotationText
101+
where
102+
text = c.getText() and
103+
annotationText = getAnnotationText(text)
104+
select c, // suppression entity
105+
text, // full text of suppression string
106+
annotationText, // LGTM suppression annotation text
102107
c.getScope() // scope of suppression
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
| TestSuppressWarnings.java:2:1:2:49 | SuppressWarnings | lgtm[java/non-sync-override] | lgtm[java/non-sync-override] | TestSuppressWarnings.java:2:1:17:5 | suppression range |
22
| TestSuppressWarnings.java:5:5:5:31 | SuppressWarnings | lgtm[] | lgtm[] | TestSuppressWarnings.java:5:5:8:5 | suppression range |
3-
| TestSuppressWarnings.java:10:5:10:57 | SuppressWarnings | lgtm[java/confusing-method-name] | lgtm[java/confusing-method-name] | TestSuppressWarnings.java:9:5:13:5 | suppression range |
3+
| TestSuppressWarnings.java:10:5:10:104 | SuppressWarnings | lgtm[java/confusing-method-name] not confusing | lgtm[java/confusing-method-name] | TestSuppressWarnings.java:9:5:13:5 | suppression range |
4+
| TestSuppressWarnings.java:10:5:10:104 | SuppressWarnings | lgtm[java/non-sync-override] | lgtm[java/non-sync-override] | TestSuppressWarnings.java:9:5:13:5 | suppression range |

java/ql/test/query-tests/AlertSuppression/TestSuppressWarnings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public void test() {
77

88
}
99
@Deprecated
10-
@SuppressWarnings("lgtm[java/confusing-method-name]")
10+
@SuppressWarnings({"lgtm[java/confusing-method-name] not confusing","lgtm[java/non-sync-override]"})
1111
public void test2() {
1212

1313
}

0 commit comments

Comments
 (0)