Java: no highlight, only fix on numeric literals of length 4 (IDEA-261998)

GitOrigin-RevId: aea6ef78149c1a2cf6d9bc3e98713c6bb9eb2035
This commit is contained in:
Bas Leijdekkers
2025-06-20 18:38:03 +00:00
committed by intellij-monorepo-bot
parent e0ff0f3d41
commit c97b9104a2
@@ -1,8 +1,9 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInspection.numeric;
import com.intellij.codeInspection.CommonQuickFixBundle;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.java.JavaBundle;
import com.intellij.pom.java.JavaFeature;
@@ -20,13 +21,13 @@ import org.jetbrains.annotations.NotNull;
public final class InsertLiteralUnderscoresInspection extends LocalInspectionTool {
@Override
public @NotNull PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
public @NotNull PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
if (!PsiUtil.isAvailable(JavaFeature.UNDERSCORES, holder.getFile())) {
return PsiElementVisitor.EMPTY_VISITOR;
}
return new JavaElementVisitor() {
@Override
public void visitLiteralExpression(final @NotNull PsiLiteralExpression literalExpression) {
public void visitLiteralExpression(@NotNull PsiLiteralExpression literalExpression) {
final PsiType type = literalExpression.getType();
if (!PsiTypes.intType().equals(type) && !PsiTypes.longType().equals(type) &&
!PsiTypes.floatType().equals(type) && !PsiTypes.doubleType().equals(type)) return;
@@ -35,15 +36,16 @@ public final class InsertLiteralUnderscoresInspection extends LocalInspectionToo
if (text == null || text.contains("_")) return;
final String converted = LiteralFormatUtil.format(text, type);
if (converted.length() == text.length()) return;
int length = text.length();
if (converted.length() == length) return;
final String displayMessage = JavaBundle.message("inspection.insert.literal.underscores.display.name");
final String actionText = CommonQuickFixBundle.message("fix.replace.x.with.y", text, converted);
final String familyName = JavaBundle.message("inspection.insert.literal.underscores.family.name");
final ConvertNumericLiteralQuickFix quickFix = new ConvertNumericLiteralQuickFix(converted, actionText, familyName);
holder.registerProblem(literalExpression, displayMessage, quickFix);
holder.registerProblem(literalExpression, JavaBundle.message("inspection.insert.literal.underscores.display.name"),
length <= 4 ? ProblemHighlightType.INFORMATION : ProblemHighlightType.GENERIC_ERROR_OR_WARNING, quickFix);
}
};
}