IntelliJ说,可能不应该作为参数x传递 [英] IntelliJ says, should probably not be passed as parameter x
问题描述
给出以下代码:
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在上面的比较行中显示警告,说:
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?
此外,如果我将比较字段更改为 left
或 right
(仅出于播放和调查目的),警告将移至第二个参数,用于例如:
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'
再次,这里有什么可疑的?为什么它抱怨字段 height
的第一个参数,以及字段 left
和 right
的第二个参数?这是什么逻辑?
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?
推荐答案
在设置中查找检查时,其描述如下:
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;
或
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.
由于 Integer.compare
的签名是 public static int compare(int x,int y)
,IntelliJ感到困惑,并认为您正在尝试传递一些语义上表示参数 x
的高度,给定名称应表示水平偏移量.
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):
这篇关于IntelliJ说,可能不应该作为参数x传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!