简体   繁体   中英

IntelliJ IDEA: What is 'Unnecessary local variable' inspection name?

I want to run certain inspection on Java project. I see this inspection working in real-time in edit window. But when I use "Analyze -> Run Inspection By Name" I can't find this inspection. Actually there is one with the same name but it's for JavaScript(tried it though, not working, as expected). Can anyone tell me exact name of inspection I need or teach me how to figure it out?

Here's code sample:

private Integer getInteger(){
    //x on the line below will be highlighted by inspection saying
    // "Local variable 'x' is redundant more... (Ctrl+F1)"
    Integer x = 5;
    return x;
}

Update: I managed to launch needed inspection using workaround. I created test case like above, then pressed Alt+Enter on highlighted region then right arrow key and selected "Run inspection on". 在此输入图像描述 This is working, but still leaves unclear how to run same inspection via "Analyze -> Run Inspection By Name"

I get the "Local variable 'i' is redundant message" (and a "yellow" analysis) when I type this code:

public int testMe()
{
   int i = 5;  // The 'i' is highlighted
   return i;
}

I can suppress this warning (and get a "green" analysis, no warnings), by adding this annotation:

@SuppressWarnings("UnnecessaryLocalVariable")
public int testMe()
{
   int i = 5;
   return i;
}

Try "Redundant Local Variable". This seems to be the description to match the annotation, according to this list of suppress warnings arguments .


Redundant local variable    UnnecessaryLocalVariable

In my IntelliJ IDEA 12.1.5 it's called Redundant local variable , I think.

Your x is redundant because you are not using that variable anywhere in that scope, besides returning it. And so that code

Integer x = 5;
return x;

can be simplified to

return 5;

Yet to avoid "magic numbers" like return 5; you could assign it to a class constant ( private static final Integer X = 5; )

It is called "Unused local variable".

EDIT: is it "Redundant local variable" you are looking for then?

这是由一个在IntelliJ IDEA 14中修复的错误引起的。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM