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

[java] Allow @SuppressWarnings with constants instead of literals #520

Closed
jsotuyod opened this issue Jul 26, 2017 · 1 comment · Fixed by #2777
Closed

[java] Allow @SuppressWarnings with constants instead of literals #520

jsotuyod opened this issue Jul 26, 2017 · 1 comment · Fixed by #2777
Labels
an:enhancement An improvement on existing features / rules
Milestone

Comments

@jsotuyod
Copy link
Member

Description:
PMD allows violation suppression through the @SuppressWarnings annotation, but such suppression currently only allows for a literal even when constants are valid in the Java grammar.

Edge cases (initialization on a static block with arbitrary code), or calls to static methods can't be properly resolved, but the most basic case can certainly be with the current infrastructure.

Code Sample demonstrating the issue:

public class NewClass {
    private final static String PMD_UNUSED_FORMAL_PARAMETER = "PMD.UnusedFormalParameter";

    @SuppressWarnings(PMD_UNUSED_FORMAL_PARAMETER)
    public void someMethod1(Object param) {
        System.out.println("someMethod1");
    }
}

Running PMD through: any

@jsotuyod jsotuyod added the an:enhancement An improvement on existing features / rules label Jul 26, 2017
@oowekyala
Copy link
Member

Edge cases (initialization on a static block with arbitrary code), or calls to static methods can't be properly resolved, but the most basic case can certainly be with the current infrastructure.

Annotation values must be constant expressions, so only inlined compile-time constants are allowed (not blank constant fields), and static methods are also prohibited. The only edge case is when the constant is defined in another compilation unit, in which case we need multifile analysis/ classpath info.

This is not a difficult problem per se but it would require a bit of logic to evaluate constant expressions. Given the current complicated expression grammar, I'd rather implement this on 7.0, with the good grammar.

@oowekyala oowekyala added this to the 7.0.0 milestone Oct 16, 2019
oowekyala added a commit to oowekyala/pmd that referenced this issue Sep 11, 2020
@adangel adangel linked a pull request Sep 17, 2020 that will close this issue
4 tasks
@adangel adangel closed this as completed Sep 17, 2020
@adangel adangel mentioned this issue Jan 23, 2023
55 tasks
@adangel adangel changed the title [java] Allow @SuppressWarnings with constants instead of literals [java] Allow @SuppressWarnings with constants instead of literals Jan 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
an:enhancement An improvement on existing features / rules
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants