mirror of
https://github.com/github/codeql.git
synced 2026-02-13 13:41:08 +01:00
1.5 KiB
1.5 KiB
Overview
An empty method may indicate that an implementation was intended to be provided but was accidentally omitted. When using the method, it will not be clear that it does not provide an implementation and with dynamic dispatch, resolving to a blank method may result in unexpected program behavior.
Recommendation
If a method is intended to be left empty, do one of the following to indicate that it is intentionally empty:
- Mark it abstract in an abstract class
- Place it in an interface (then it can be implicitly abstract)
- Place a comment in that method that lets others know that the implementation was intentionally omitted
- Add
UnsupportedOperationExceptionto the method (as injava.util.Collection.add).
Example
public class Test {
public void f1() { // COMPLIANT
// intentionally empty
}
public void f2() {} // NON_COMPLIANT
public void f3(){ throw new UnsupportedOperationException(); } // COMPLIANT
public abstract class TestInner {
public abstract void f(); // COMPLIANT - intentionally empty
}
}
Implementation Notes
The rule excludes reporting methods that are annotated.
References
- Java SE Documentation: java.util.Collection.add.
- Wikipedia: Template method pattern.
- Common Weakness Enumeration: CWE-1071.