29

When a method is never used, IntelliJ shows a warning Method xxx() is never used

But for POJOs, I create Getters/Setters for every attributes and I do not want IntelliJ to warn me for unused Getters/Setters.

Is there a way to automatically suppress those warnings?

Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
  • I am wondering, how IntelliJ could know, that a getter/setter is not used? Usually they are public! In Eclipse, we have the same kind of warning, that only pops up, when a method is private. – Seelenvirtuose Oct 14 '14 at 08:24
  • @Seelenvirtuose IntelliJ builds a `Dependencies Structure Matrix` which enables to find every reference of a method/class/attribute etc. see https://www.jetbrains.com/idea/features/dependency_analysis.html#link8 – Arnaud Denoyelle Oct 14 '14 at 08:32
  • 1
    Eclipse does nearly the same (it builds an AST). The main point of my comment is: You cannot know, whether your class is maybe part of a library used in other projects, where those methods are used. So you should not display a warning for an unused method that is public. It's another story if it's private. – Seelenvirtuose Oct 14 '14 at 08:35
  • @Seelenvirtuose I agree with you. IntelliJ cannot guess if the developer is making a library or an application. Hence, the developer cannot rely on it when making a library but this is extremely useful when developing an application. – Arnaud Denoyelle Oct 14 '14 at 08:42
  • @Seelenvirtuose and thank you for AST. Now I understand what is this cryptic `AST Node` that I often see in exceptions :) – Arnaud Denoyelle Oct 14 '14 at 08:43

4 Answers4

21

As of IDEA 2016.3 there is a setting to do this. Using IntelliJ on a Mac, it is under Settings | Editor | Inspections | Java | Declaration redundancy | Unused declaration | Methods | Getters/setters

Disable unused getter/setter method warnings

TerekC
  • 2,133
  • 3
  • 20
  • 22
1

I believe you can annotate your methods with //noinspection.

You can navigate to Settings | Inspections | Declaration redundancy | Unused Declarations and define a scope for your methods but I do believe you cannot disable it for getters/setters only an leave other methods out of inspection.

tmarwen
  • 15,750
  • 5
  • 43
  • 62
1

I'd recommend lowering the severity for unused methods in general. Simply go to Settings | Inspections | Declaration redundancy | Unused Declarations, and change the severity to "Info" or "Weak Warning" .

BZapper
  • 320
  • 3
  • 19
0

You have just to hit Alt+Enter and select Suppress for methods annotated by ...

Where to click

See also: Telling IntelliJ IDEA which methods not to identify as unused

Community
  • 1
  • 1
snailer
  • 62
  • 6
  • 1
    The Getters/Setters do not have any annotations. This forbids me to select them by annotation. But your answer solves another of my problems : suppress warnings for methods annotated with `com.google.inject.Inject` – Arnaud Denoyelle Oct 14 '14 at 08:35
  • 4
    This would mean annotating each getter/setter, so we could initially add a "suppress" annotation. It's not helpful. – Aviv Carmi Nov 07 '16 at 08:19