fb-contrib icon indicating copy to clipboard operation
fb-contrib copied to clipboard

WOC_WRITE_ONLY_COLLECTION_FIELD for constant collections

Open romani opened this issue 2 years ago • 1 comments

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

romani avatar Dec 03 '23 15:12 romani

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

vbrandl avatar Dec 15 '23 17:12 vbrandl