-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathUnusedField.ql
More file actions
31 lines (29 loc) · 1.07 KB
/
UnusedField.ql
File metadata and controls
31 lines (29 loc) · 1.07 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
/**
* @name Unused field
* @description A field that is never used is probably unnecessary.
* @kind problem
* @problem.severity recommendation
* @precision low
* @id java/unused-field
* @tags maintainability
* useless-code
* external/cwe/cwe-561
*/
import java
import semmle.code.java.Reflection
import semmle.code.java.frameworks.Lombok
from Field f
where
not (f.isPublic() or f.isProtected()) and
f.fromSource() and
not f.getDeclaringType() instanceof EnumType and
not exists(VarAccess va | va.getVariable().(Field).getSourceDeclaration() = f) and
// Exclude results in generated classes.
not f.getDeclaringType() instanceof GeneratedClass and
// Exclude fields that may be reflectively read (this includes standard serialization).
not reflectivelyRead(f) and
// Exclude fields with deliberately suppressed warnings.
not f.suppressesWarningsAbout("unused") and
// Exclude fields with relevant Lombok annotations.
not f instanceof LombokGetterAnnotatedField
select f, "Unused field " + f.getName() + " in " + f.getDeclaringType().getName() + "."