Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WOC_WRITE_ONLY_COLLECTION_FIELD for constant collections #453

Open
romani opened this issue Dec 3, 2023 · 1 comment
Open

WOC_WRITE_ONLY_COLLECTION_FIELD for constant collections #453

romani opened this issue Dec 3, 2023 · 1 comment

Comments

@romani
Copy link

romani commented Dec 3, 2023

continuation of #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
Copy link

vbrandl commented Dec 15, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants