SonarQube 正在报告删除此始终计算为“true”的表达式

SonarQube is reporting Remove this expression which always evaluates to “true”

我有以下代码:

final Set<String> desktopMediaCodes = getCodesByMediaDeviceType(mediaModels, MediaDeviceType.DESKTOP);
final Set<String> mobileMediaCodes = getCodesByMediaDeviceType(mediaModels, MediaDeviceType.MOBILE);
final Set<String> tabletMediaCodes = getCodesByMediaDeviceType(mediaModels, MediaDeviceType.TABLET);

//In case they are the same, only default.
if (desktopMediaCodes.equals(mobileMediaCodes) && mobileMediaCodes.equals(tabletMediaCodes)) {
    asset.setDefaults(desktopMediaCodes);
    return;
}

//In case three are different, we will send mobile, desktop and tablet.
if(!desktopMediaCodes.equals(mobileMediaCodes) && !desktopMediaCodes.equals(tabletMediaCodes) && !mobileMediaCodes.equals(tabletMediaCodes)){
    asset.setDesktop(desktopMediaCodes);
    asset.setMobile(mobileMediaCodes);
    asset.setTablet(tabletMediaCodes);
    return;
}

//In case only tablet is different, we will send default and tablet.
if(desktopMediaCodes.equals(mobileMediaCodes) && !mobileMediaCodes.equals(tabletMediaCodes)){
    asset.setDefaults(desktopMediaCodes);
    asset.setTablet(tabletMediaCodes);
    return;
}

//In case only desktop is different, we will send default and tablet.
if(mobileMediaCodes.equals(tabletMediaCodes) && !tabletMediaCodes.equals(desktopMediaCodes)){
    asset.setDefaults(mobileMediaCodes);
    asset.setDesktop(desktopMediaCodes);
    return;
}

//In case only mobile is different, we will send default and tablet.
if(tabletMediaCodes.equals(desktopMediaCodes) && !tabletMediaCodes.equals(mobileMediaCodes)){
    asset.setDefaults(tabletMediaCodes);
    asset.setMobile(mobileMediaCodes);
    return;
}

SonarQube 向我报告

(黄色字段)

这不是真的,这是一个错误还是我遗漏了什么? SonarQube 让我很困惑。

您需要阅读整个上下文。您的代码中有 5 个 if 语句。我会稍微简化一下:

1) D == M && M == T

2) D != M && D != T && M != T

3) D == M && M != T

4) M == T && D != T

5) D == T && D != M

我们来分析一下代码。

第一期

它告诉您 M != T 总是在第 3 行求得 true

3) D == M && M != T

为什么?因为你的第一行是:

1) D == M && M == T

它保证在第 3 行中只有以下值是可能的:

D == M && M != T
D != M && M != T
D != M && M == T

你可以理解为:

  • if D == M then M must be != T (没有意义检查 M != T)
  • if D != M then M may be = or != T (we have to check if M = or != T)

新密码是:

1) D == M && M == T

2) D != M && D != T && M != T

3) D == M

4) M == T && D != T

5) D == T && D != M

第二期

和第一期一样的情况。 D != T 总是在第 4 行求得 true

4) M == T && D != T

因为第 3 行:

3) D == M

保证 D != M。第二行:

2) D != M && D != T && M != T

保证:

D == T && M != T
D != T && M == T
D == T && M == T

我们将删除第三个选项,因为第一行捕获了它:

1) D == M && M == T

现在我们有:

D == T && M != T
D != T && M == T

你可以理解为:

  • if D == M then M must be != T (没有意义检查 M != T)
  • if D != M then M must be = T (没有意义检查 M == T)

新密码是:

1) D == M && M == T

2) D != M && D != T && M != T

3) D == M

4) M == T

5) D == T && D != M

第三、四期

整个 if 条件的计算结果总是 true。让我们阅读第 5 行:

5) D == T && D != M

M != D 始终为真,因为第 3 行保证它:

3) D == M

新密码是:

1) D == M && M == T

2) D != M && D != T && M != T

3) D == M

4) M == T

5) D == T

我们也可以删除 D == T 因为:

3) D == M
4) M == T

在第 5 行,只有以下选项是可能的:D != M && M != T。第二行是:

2) D != M && D != T && M != T

If D != T 则第 2 行捕获它。这意味着唯一的可能性是D == T,所以检查它没有意义。

最终代码

final Set<String> desktopMediaCodes = getCodesByMediaDeviceType(mediaModels, MediaDeviceType.DESKTOP);
final Set<String> mobileMediaCodes = getCodesByMediaDeviceType(mediaModels, MediaDeviceType.MOBILE);
final Set<String> tabletMediaCodes = getCodesByMediaDeviceType(mediaModels, MediaDeviceType.TABLET);

// In case they are the same, only default.
if (desktopMediaCodes.equals(mobileMediaCodes) && mobileMediaCodes.equals(tabletMediaCodes)) {
    asset.setDefaults(desktopMediaCodes);
    return;
}

// In case three are different, we will send mobile, desktop and tablet.
if (!desktopMediaCodes.equals(mobileMediaCodes) && !desktopMediaCodes.equals(tabletMediaCodes) && !mobileMediaCodes.equals(tabletMediaCodes)) {
    asset.setDesktop(desktopMediaCodes);
    asset.setMobile(mobileMediaCodes);
    asset.setTablet(tabletMediaCodes);
    return;
}

// In case only tablet is different, we will send default and tablet.
if (desktopMediaCodes.equals(mobileMediaCodes)) {
    asset.setDefaults(desktopMediaCodes);
    asset.setTablet(tabletMediaCodes);
    return;
}

// In case only desktop is different, we will send default and tablet.
if (mobileMediaCodes.equals(tabletMediaCodes)) {
    asset.setDefaults(mobileMediaCodes);
    asset.setDesktop(desktopMediaCodes);
    return;
}

// In case only mobile is different, we will send default and tablet.
asset.setDefaults(tabletMediaCodes);
asset.setMobile(mobileMediaCodes);