I have a Java code line where IntelliJ displays a warning. How do I silence the warning in that particular line, without affecting warnings displayed in other lines?
In this question it's irrelevant what the actual warning is: now I'm not seeking advice on how to improve the quality of a specific piece of Java code, but I want to know in general how to prevent IntelliJ from displaying a warning on a specific Java source line.
Mostly in IntelliJ, you can click on the line and Alt+Enter, and it will have options for suppressing the warning, among other things.
Expanding upon Ryan Stewart's answer, in IntelliJ, use Alt+Enter, then select the first sub-menu, then the last item: Suppress for statement.
Update
Using IntelliJ IDEA 13, I noticed an additional menu item: "Suppress for statement with comment". This will use the IntelliJ style //noinspection unchecked. If you select "Suppress for statement", IntelliJ will insert this text: #SuppressWarnings("unchecked").
Depending on the warning you can use #SuppressWarnings. Eg:
#SuppressWarnings("deprecation")
yourLineWhichIsDeprecated;
Take a look at this answer for a pretty long list of warnings you can suppress. Check the other posts on that topic for more details.
In IntelliJ 15 the inline "yellow bulb" and alt-enter menus don't offer to suppress the inspection for 1 line.
There are more options when running the inspections via the Menu: Analyze -> Inspect Code....
Then on the Inspection panel the right side offers various options.
Some of text in the right hand panel is clickable. Note that usually the problem resolution function (quick fix) is also available.
(Apparently #tino already noticed this in a comment, but I missed his comment the first time. I'm adding this as full answer to make the important bit I personally missed easier to find.)
Just one more note. If you are looking for a answer to suppress all warnings for a next line (or part of the code). It might be a reason for a several cases:
Idea doesn't provide error name, or suggestions
Number of warnings for next line is too large
You can use just:
//noinspection ALL
Click on the complaining area that has a wavy line 〰️ beneath it
A light bulb 💡 appears, click the light bulb 💡
Select the suppress statement option
4. An no inspection comment will be added above current line //noinspection CssInvalidPropertyValue
The complaining disappear 👏
Problems panel
The Problems panel shows a list of warnings and errors in our code. There we can peruse the various issues with our code, working through the list one-by-one. This feature arrived in IntelliJ 2020.2.
In at least IntelliJ 2021.2, and perhaps earlier, we can suppress a warning within that panel.
When selecting a problem point, the right-side pane of the Problems panel shows a Suppress widget. This pop-up menu displays items for various ways to suppress the warning.
When compiling code using Kotlin language and IntelliJ here is a hint
Here is a link to the github source where the compiler warnings have their origin and where the default error messages output by the Kotlin compiler are defined
kotlin/compiler/frontend/src/org/jetbrains/kotlin/diagnostics/rendering/DefaultErrorMessages.java
If the compiler outputs "Variable ''{0}'' is never used" it origins from this line form DefaultErrorMessages.java
MAP.put(UNUSED_VARIABLE, "Variable ''{0}'' is never used", NAME);
To suppress the warning you can put a #Suppress() annotation before an unused variable in your code like this:
#Suppress("UNUSED_VARIABLE")
var y: Int = 3
So the trick if the IntelliJ does not help you pop up suggestions pressing Alt+ENTER at a highlighted expression is to look inside DefaultErrorMessages.java if you can find the error message and the keyword to supress a particular warning using #Suppress(..names)
This topic is not marked "Kotlin" but at least marked IntelliJ
//noinspection unchecked,ConstantConditions
#SuppressWarnings does not work in every place
As other questions point out, there are several options to achieve what are you asking for. A more comprehensive list of the options available can be found in the official documentation here.
Related
I am new in IntelliJ IDEA and use a line as shown below:
List<UUID> employeeUuidList = employeeService.findAllEmployeesByMainUuid(request.getMainUuid());
Interestingly, it gives "Duplicated code fragment (19 lines long)" error. I have a look at on the web, there are some suggestion to suppress this warning, but I want to understand the main cause of this warning and fix it by code side without changing setting.
It is also ok if you give suggestion for suppressing this warning.
Duplicated code fragment (19 lines long)
This is neither a error nor a serious warning. It is just a highlight from intelliJ that those 19 lines are repeated elsewhere.
You could probably refactor the code by extracting those duplicate lines to a common method.
If you want to disable it completely (I don't recommend though), goto preferences > Editor > Duplicates or simply search duplicate in the preferences and there you can enable/disable this feature for a language.
Attached screenshot:
"Duplicated code fragment" is the warning triggered by the inspection: General | Duplicated code fragment. To see the duplicates you may press "Alt+Enter" (in the line where the warning appears) and select the "Show all duplicates like this":
I know how to jump to the next and previous error (F2, see SO Link, jetbrains.com), but is there a way to easily jump between suggestions?
If possible, I would really love to have a keyboard shortcut for this, as I am currently learning Kotlin and IntelliJ often has nice suggestions on how to improve the code.
If there is no keyboard shortcut to jump between suggestions, is there at least any other way?
With suggestion I mean things like the following (Ignore the "nonsense code")):
F2 also works to navigate between warnings. If there are errors in the file, it will jump to the next error. If not, it will jump to the next warning.
Adding to yole's answer: if you want to jump backwards you can use Shift + F2 instead.
Additionally you can alter the severity of each inspection (or inspection group) using the settings menu File | Settings | Editor | Inspections or directly using the intention action context menu (ALT + ENTER or similar) when dealing with a highlighted element (be it warning, error, information, etc.).
Note that some inspections are not enabled by default and many allow customization to your needs (e.g. class naming conventions, etc.).
I did once increase the severity of all the inspections and switch them back one by one as needed or even turned many off (or switched them to "weak warnings"), just to easily jump back and forth between them. I can't recommend that approach anymore ;-) (too many highlightings then... but it may help to find out about inspections you consider more helpful).
I use many brackets and braces when I code. Be it casting multiple times, casting multiple times in if blocks, etc. I sometimes get lost in the brackets and also, hate putting a lot of them.
Is there any short-cut key to format this selected part of code?
I have tried Ctrl-Shift-F, but that doesn't give me what I want.
Note: I work on Eclipse Mars.
What you want is Source menu | Cleanup, then customize the profile to add the remove extra parenthesis. From the Code Style tab check the Use parenthesis in expressions Only if necessary. And then complete the wizard:
That changes:
super.start(((BundleContext)(context)));
to:
super.start(context);
As an extra you can set your project to do code cleanup tasks on save automatically if you desire.
did you try ctrl+3 and type formate? for me Ctrl-Shift-F was bound by other app and cause eclipse to miss catching it.
search for formatter Java->code style->formatter
There is a tab Braces.
If you select specific lines and press Ctrl + Shift + F eclipse will only format the lines you selected according to your formatter.
I find this extremely annoying that you often do not get help with figuring out the parameter signatures of methods and contructors when you have already written the first parameter.
Instead Intellij will show the variables available to you in your context. This is good, but not the first prio. I want to see the signature of the method/constructor as well.
This has the consequence that you always have to keep deleting and CTRL + SPACE to see the signature.
Is there a way around this issue?
You don't have to press Ctrl+P, you can configure IDEA to always show this information:
Go to Settings > Editor > General > Code Completion
Select the Show full signatures and Autopopup in (ms:) boxes.
You can press Ctrl+P to show the signature(s) of the method/constructor, see this link for more details:
https://www.jetbrains.com/idea/webhelp/viewing-method-parameter-information.html
I've been recently using sonar for code analysis. When I go thorough violation drilldown, I found many java files with if statement defined without braces (thousands of places). Is there a simple way to replace or add braces to if statements or what are the steps that I can perform to achieve this task without doing it manually in each of the files.
I'm currently using intelliJ.
Is there a simple way to replace or add braces to if statements or what are the steps that I can perform to achieve this task without doing it manually in each of the files.
I don't know if there is a tool to do this automatically. (There probably is ...) But assuming that such a tool exists, I'm not convinced it would be the right approach.
Step back for a moment and consider why the code analysis has reported this as a problem. A lot of people (like #pst and me) think that the braces always should be there, even though various style guides don't insist on this. There is a good reason for this ... apart from "it looks ugly". Consider these example code snippets:
if (i == 1)
i++;
doSomething();
while (i < 1)
i++;
doSomething();
If you don't read those carefully, your eyes will trick you into thinking that doSomething(); is called conditionally ... due to the incorrect indentation. And it happens.
(Aside: labelling someone as "inept" for misreading that code is not helpful. If you are desperately trying to fix a show-stopper bug and you've been working for 14 hours straight, then you are likely to miss this kind of thing. And not because you are inept. Once you've been in that situation a couple of times, the lesson sinks in ...)
OK, now suppose that you run an automatic tool to add the braces. What you will get is this:
if (i == 1) {
i++;
}
doSomething();
while (i < 1) {
i++;
}
doSomething();
It means exactly the same thing as the original code. BUT ... what if the original code was actually a bug? What if the programmer intended the doSomething() calls to be conditional?
In short, by adding the braces automatically, we've obscured the original programmer's intention, and made these bug(s) harder to track down.
Bottom line - I think it would be prudent to manually review each of these occurrences ... rather than just "fixing" them automatically. Indeed, I'd argue that if you don't have the time or patience to review them manually, it would be better to leave the code alone. It would be better to turn off the warning ... IMO.
I can reformat the code to make intelliJ do the thing for me, but I need to go through all the files and reformat it. Yes, I might turned off the check but wondering if there is a good tool to do the task. I've good set of tests to check whether it introduce bugs during the process.
If you are sure that you have some ways to test that you will not introduce bugs then use the IntelliJ Reformat Code feature.
Just make sure that the Code Style you have in IntelliJ is in line with your company's policies. Otherwise you will force your style on everybody else too.
To force braces just mark them as Always on the Wrapping and Braces tag in the Code Style settings dialog in IntelliJ.
Mark the source folder in the project view and press Ctrl-Alt-L. A dialog pops up and there you can chose All files in directory <...>.
Then press Run and see what happens. If you are not satisfied then just revert from your VCS.
IntelliJ IDEA has an inspection for this as well, and it has a quick fix to automatically add the braces.
Invoke Analyze | Run Inspection by Name and enter inspection name Control flow statement without braces. Run it on the desired part of your project. In the inspection results you can apply the quick fix Add braces to statement.
Note that this inspection will also report control flow statement other than if, like for example while statements. Invoking the quick fix will also add braces to those statements.