|
| 1 | +/** |
| 2 | + * Finds usages of Lombok's `@EqualsAndHashCode(callSuper = true)` where |
| 3 | + * none of the superclasses override `equals` or `hashCode`. This means the |
| 4 | + * methods generated by Lombok will call `Object.equals` or `Object.hashCode`, |
| 5 | + * which is likely not desired since they check for object identity. |
| 6 | + * |
| 7 | + * Lombok can detect this issue itself and aborts compilation, but only when |
| 8 | + * `Object` is the direct superclass (respectively when no superclass is specified). |
| 9 | + * In other cases Lombok cannot detect this issue yet. |
| 10 | + * |
| 11 | + * @kind problem |
| 12 | + */ |
| 13 | + |
| 14 | +// TODO: This query might be redundant; the CodeQL Java extractor seems to process the |
| 15 | +// code generated by Lombok, so the errors detected by this query are also detected |
| 16 | +// by `equals-hashCode-calling-Object-super.ql` |
| 17 | + |
| 18 | +import java |
| 19 | + |
| 20 | +from RefType annotated, Annotation annotation, string notOverridden |
| 21 | +where |
| 22 | + annotation = annotated.getAnAnnotation() and |
| 23 | + annotation.getType().hasQualifiedName("lombok", "EqualsAndHashCode") and |
| 24 | + annotation.getBooleanValue("callSuper") = true and |
| 25 | + ( |
| 26 | + // No superclass overrides `equals` |
| 27 | + notOverridden = "equals" and |
| 28 | + not exists(Class superclass | |
| 29 | + superclass = annotated.getASourceSupertype+() and |
| 30 | + not superclass instanceof TypeObject and |
| 31 | + superclass.getAMethod() instanceof EqualsMethod |
| 32 | + ) |
| 33 | + or |
| 34 | + // Or no superclass overrides `hashCode` |
| 35 | + notOverridden = "hashCode" and |
| 36 | + not exists(Class superclass | |
| 37 | + superclass = annotated.getASourceSupertype+() and |
| 38 | + not superclass instanceof TypeObject and |
| 39 | + superclass.getAMethod() instanceof HashCodeMethod |
| 40 | + ) |
| 41 | + ) |
| 42 | +select annotation, "Specifies `callSuper=true` but superclasses don't override `" + notOverridden + "`" |
0 commit comments