An import repeats another or names a class the file sees anyway
What it finds
Some imports change nothing: a class of java.lang, which every file sees, a class of the file's own package, and an import that repeats an earlier one. They only lengthen the list a reader goes through. The fix removes the import.
Classes in subpackages of java.lang, such as java.lang.reflect, need their imports and are not reported.
From the rule's source, needless_import.rs.
package com.example.shop; import java.lang.String;tx0 reports: the import of java.lang.String is not needed: classes of java.lang are visible in every fileimport java.lang.reflect.Method;import java.util.List;import java.util.List;tx0 reports: the import of java.util.List is not needed: it repeats an earlier importimport com.example.shop.Cart;tx0 reports: the import of com.example.shop.Cart is not needed: the class is in the file's own packageimport com.example.shop.model.Item; class Checkout { String name; Method method; List<Item> items; Cart cart;} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:needless-import" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:needless-import] reason="..."tx0 explain tx0/java:needless-import shows the same in the terminal. Found a false positive? Report it.