spotbugs / spotbugs

SpotBugs is FindBugs' successor. A tool for static analysis to look for bugs in Java code.
https://spotbugs.github.io/
GNU Lesser General Public License v2.1
3.39k stars 581 forks source link

ignore @Generated, and a config option to filter by annotation #1385

Open xenoterracide opened 3 years ago

xenoterracide commented 3 years ago

I think that spotbugs should auto ignore (or be easily configurable to) classes/methods/fields decorated with @javax.annotation.processing.Generated, also some frameworks don't use this, so honestly I think it should ignore those things if the name of the annotation is Generated. Alternatively, maybe just add an option to the exclude/filter config, e.g.

    <Match>
        <Bug code="L,D,BC"/>
        <Annotation name="org.immutables.value.Generated"/>
    </Match>
marquiswang commented 3 years ago

Would love to have this as well. Using Dagger and Immutables, which lead to many false positives (or at least, not fixable by me positives).

Lucas3oo commented 1 year ago

+1 same with Micronaut generated code. Those have @io.micronaut.core.annotation.Generated

RenovatingDev commented 11 months ago

Just wanted to start working on this and found that it has already been done. This issue should be covered by https://github.com/spotbugs/spotbugs/pull/2395 and should therefore be included in the next release.

As far as I can see, the corresponding documentation in docs/filter.rst is missing though. I could add some, are there other places where they would be needed? @hazendaz

hazendaz commented 10 months ago

@RenovatingDev Not sure where all documentation is missing, please do send PR for anything you find.

sergeykad commented 8 months ago

I am trying to use the new option, but it does not work.

https://github.com/spotbugs/spotbugs/blob/4.8.0/CHANGELOG.md#added

<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter
        xmlns="https://github.com/spotbugs/filter/3.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubusercontent.com/spotbugs/spotbugs/4.8.0/spotbugs/etc/findbugsfilter.xsd">
    <Match>
        <Annotation name="javax.annotation.processing.Generated"/>
    </Match>
</FindBugsFilter>
xenoterracide commented 3 months ago

seems like it's working for me,

<?xml version="1.0" encoding="UTF-8" ?>
<FindBugsFilter
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="https://github.com/spotbugs/filter/3.0.0"
  xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0  https://raw.githubusercontent.com/spotbugs/spotbugs/4.8.0/spotbugs/etc/findbugsfilter.xsd"
>
  <Match>
    <!-- Returning mutable references, I slightly agree but this is also ludicrous level -->
    <Bug pattern="EI_EXPOSE_REP" />
  </Match>
  <Match>
    <Bug pattern="UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR" />
    <Class name="~.*Test.*" />
  </Match>
  <Match>
    <!-- safe to ignore in tests due to gitattributes -->
    <Bug pattern="VA_FORMAT_STRING_USES_NEWLINE" />
    <Class name="~.*Test$?.*" />
  </Match>
  <Match>
    <Annotation name="jakarta.persistence.metamodel.StaticMetamodel" />
  </Match>
</FindBugsFilter>
tlefevre commented 1 month ago

Doesn't work for me either. It seems it only works if the class itself has the annotation and won't ignore generated methods with a Generated annotation for example.

Vampire commented 1 month ago

Yes, that's correct. That's what the above linked release notes tell. If you want it to match on methods, I guess you should post a separate feature request.