forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteOnlyContainer.ql
More file actions
46 lines (44 loc) · 1.76 KB
/
WriteOnlyContainer.ql
File metadata and controls
46 lines (44 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* @name Container contents are never accessed
* @description A collection or map whose contents are never queried or accessed is useless.
* @kind problem
* @problem.severity error
* @precision very-high
* @id java/unused-container
* @suites security-and-quality
* quality
* @tags maintainability
* useless-code
* external/cwe/cwe-561
*/
import java
import semmle.code.java.Reflection
import semmle.code.java.frameworks.Lombok
import Containers
from Variable v
where
v.fromSource() and
v.getType() instanceof ContainerType and
// Exclude parameters and non-private fields.
(v instanceof LocalVariableDecl or v.(Field).isPrivate()) and
// Exclude fields that may be read from reflectively.
not reflectivelyRead(v) and
// Exclude fields annotated with `@SuppressWarnings("unused")`.
not v.getAnAnnotation().(SuppressWarningsAnnotation).getASuppressedWarning() = "unused" and
// Exclude fields with relevant Lombok annotations.
not v instanceof LombokGetterAnnotatedField and
// Every access to `v` is either...
forex(VarAccess va | va = v.getAnAccess() |
// ...an assignment storing a new container into `v`,
exists(AssignExpr assgn |
va = assgn.getDest() and assgn.getSource() instanceof ClassInstanceExpr
)
or
// ...or a call to a mutator method on `v` such that the result of the call is not checked.
exists(ContainerMutation cm | va = cm.getQualifier() and not cm.resultIsChecked())
) and
// Also, any value that `v` is initialized to is a new container,
forall(Expr e | e = v.getAnAssignedValue() | e instanceof ClassInstanceExpr) and
// and `v` is not implicitly initialized
not v.(LocalVariableDecl).getDeclExpr().hasImplicitInit()
select v, "The contents of this container are never accessed."