Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ says, should probably not be passed as parameter x

Given this code:

private static class Building {
    private final int left;
    private final int right;
    private final int height;

    private Building(int left, int right, int height) {
        this.left = left;
        this.right = right;
        this.height = height;
    }
}

private PriorityQueue<Building> createMaxHeapByHeight() {
    return new PriorityQueue<>(new Comparator<Building>() {
        @Override
        public int compare(Building o1, Building o2) {
            return -Integer.compare(o1.height, o2.height);
        }
    });
}

IntelliJ shows a warning for the comparison line above, saying:

return -Integer.compare(o1.height, o2.height);
//                      ^^^^^^^^^
//                      'height' should probably not be passed as parameter 'x'

The warning can be suppressed with a comment on the statement:

//noinspection SuspiciousNameCombination

Ok, but what is so suspicious here?

Also, if I change the compared field to left or to right (just for the sake of playing and investigating), the warning shifts to the second parameter, for example:

return -Integer.compare(o1.right, o2.right);
//                                ^^^^^^^^
//                                'right' should probably not be passed as parameter 'y'

Again, what is so suspicious here? Why does it complain about the first parameter for the field height, and about the second parameter for the fields left and right? What's the logic here?

like image 420
janos Avatar asked Dec 20 '15 09:12

janos


2 Answers

When you look up the inspection in settings, its description states following:

Reports assignments and function calls where the name of the variable to which a value is assigned or the function parameter does not seem to match the name of the value assigned to it. For example:

var x = 0;
var y = x;

or

var x = 0, y = 0;
var rc = new Rectangle(y, x, 20, 20);

The configuration pane allows to specify the names which should not be used together: the error is reported if the parameter name or assignment target name contains words from one group and the name of the assigned or passed variable contains words from a different group.

Because the signature of Integer.compare is public static int compare(int x, int y), IntelliJ gets confused and thinks that you are trying to pass something that semantically represents height to a parameter x that should probably represent some horizontal offset, given its name.

You can remove the group of these names from the inspection settings to fix this (or disable the inspection entirely):

enter image description here

like image 87
Bohuslav Burghardt Avatar answered Nov 15 '22 03:11

Bohuslav Burghardt


You can see the logic here: https://github.com/JetBrains/intellij-community/blob/210e0ed138627926e10094bb9c76026319cec178/java/java-analysis-impl/src/com/intellij/codeInspection/suspiciousNameCombination/SuspiciousNameCombinationInspectionBase.java

The relevant chunk is this:

public SuspiciousNameCombinationInspection() {
    addNameGroup("x,width,left,right");
    addNameGroup("y,height,top,bottom");
}

x is deemed compatible with width, left and right, but not with height (and vice versa).

like image 32
Oliver Charlesworth Avatar answered Nov 15 '22 05:11

Oliver Charlesworth