mebigfatguy / fb-contrib

a FindBugs/SpotBugs plugin for doing static code analysis for java code bases
http://fb-contrib.sf.net
GNU Lesser General Public License v2.1
157 stars 45 forks source link

WOC_WRITE_ONLY_COLLECTION_FIELD for constant collections #453

Open romani opened 11 months ago

romani commented 11 months ago

continuation of https://github.com/mebigfatguy/fb-contrib/issues/408

public abstract class AbstractClassCouplingCheck extends AbstractCheck {

    private static final char DOT = '.';

    private static final Set<String> DEFAULT_EXCLUDED_CLASSES = Set.of(
        // reserved type name
        "var",
        // primitives

        "OptionalDouble", "OptionalInt", "OptionalLong",
        // java.util.stream.*
        "DoubleStream", "IntStream", "LongStream", "Stream"
    );

    private static final Set<String> DEFAULT_EXCLUDED_PACKAGES = Collections.emptySet();

    private static final Pattern BRACKET_PATTERN = Pattern.compile("\\[[^]]*]");

    private final List<Pattern> excludeClassesRegexps = new ArrayList<>();

    private final Map<String, String> importedClassPackages = new HashMap<>();

    private final Deque<ClassContext> classesContexts = new ArrayDeque<>();

    private Set<String> excludedClasses = DEFAULT_EXCLUDED_CLASSES;

    private Set<String> excludedPackages = DEFAULT_EXCLUDED_PACKAGES;

    private int max;

    private String packageName;

    protected AbstractClassCouplingCheck(int defaultMax) {
        max = defaultMax;
        excludeClassesRegexps.add(CommonUtil.createPattern("^$"));
    }
.....

full code at https://github.com/checkstyle/checkstyle/blob/7f50a149922ba81ae867f967e8f146b3fa42097a/src/main/java/com/puppycrawl/tools/checkstyle/checks/metrics/AbstractClassCouplingCheck.java#L56

error messages:

[INFO] --- spotbugs-maven-plugin:4.7.3.5:check (default) @ checkstyle ---
[INFO] BugInstance size is 3
[INFO] Error size is 0
[INFO] Total bugs: 3
[ERROR] Medium: Class com.puppycrawl.tools.checkstyle.checks.metrics.AbstractClassCouplingCheck creates and initializes a collection but never reads or gains information from it [com.puppycrawl.tools.checkstyle.checks.metrics.AbstractClassCouplingCheck] In AbstractClassCouplingCheck.java WOC_WRITE_ONLY_COLLECTION_FIELD
[ERROR] Medium: Class com.puppycrawl.tools.checkstyle.checks.metrics.AbstractClassCouplingCheck creates and initializes a collection but never reads or gains information from it [com.puppycrawl.tools.checkstyle.checks.metrics.AbstractClassCouplingCheck] In AbstractClassCouplingCheck.java WOC_WRITE_ONLY_COLLECTION_FIELD
[ERROR] Medium: Class com.puppycrawl.tools.checkstyle.checks.metrics.AbstractClassCouplingCheck creates and initializes a collection but never reads or gains information from it [com.puppycrawl.tools.checkstyle.checks.metrics.AbstractClassCouplingCheck] In AbstractClassCouplingCheck.java WOC_WRITE_ONLY_COLLECTION_FIELD
vbrandl commented 11 months ago

Since #408 won't be reopened and if it helps, here is the testcase from #408: https://github.com/vbrandl/fb-contrib-collections-testcase