[uast-inspection] IDEA-347890 Similar logs inspection. Skip warning if they are in the same codeblock

GitOrigin-RevId: 7b481eebc5196909c648b46051bfeb46feab6d59
This commit is contained in:
Mikhail Pyltsin
2024-02-29 16:32:38 +01:00
committed by intellij-monorepo-bot
parent 165bbb3559
commit 1da47e479f
2 changed files with 43 additions and 2 deletions

View File

@@ -122,7 +122,8 @@ class LoggingSimilarMessageInspection : AbstractBaseUastLocalInspectionTool() {
val alreadyHasWarning = mutableSetOf<Int>()
for (firstIndex in 0..currentGroup.lastIndex) {
for (secondIndex in firstIndex + 1..currentGroup.lastIndex) {
if (similar(currentGroup[firstIndex].parts, currentGroup[secondIndex].parts, myMinTextLength)) {
if (similar(currentGroup[firstIndex].parts, currentGroup[secondIndex].parts, myMinTextLength) &&
!sequenceOfCalls(currentGroup[firstIndex].call, currentGroup[secondIndex].call)) {
if (alreadyHasWarning.add(firstIndex)) {
registerProblem(holder, currentGroup[firstIndex].call, currentGroup[secondIndex].call)
}
@@ -138,6 +139,24 @@ class LoggingSimilarMessageInspection : AbstractBaseUastLocalInspectionTool() {
return super.visitFile(node)
}
private fun sequenceOfCalls(call1: UCallExpression, call2: UCallExpression): Boolean {
val commonParent = PsiTreeUtil.findCommonParent(call1.sourcePsiElement, call2.sourcePsiElement)?.toUElement()
if (commonParent is UBlockExpression || commonParent is UMethod) {
val uastParent1 = call1.uastParent?.uastParent
val uastParent2 = call2.uastParent?.uastParent
if (uastParent1 == commonParent ||
(uastParent1?.uastParent is UIfExpression && uastParent1.uastParent?.uastParent == commonParent) ||
(uastParent1 is UIfExpression && uastParent1.uastParent == commonParent) ||
uastParent2 == commonParent ||
(uastParent2?.uastParent is UIfExpression && uastParent2.uastParent?.uastParent == commonParent) ||
(uastParent2 is UIfExpression && uastParent2.uastParent == commonParent)
) {
return true
}
}
return false
}
private fun collectCalls(file: UFile): Set<UCallExpression> {
val result = mutableSetOf<UCallExpression>()
file.accept(object : AbstractUastVisitor() {

View File

@@ -207,7 +207,10 @@ class JavaLoggingSimilarMessageInspectionTest : LoggingSimilarMessageInspectionT
<weak_warning descr="Similar log messages">logger.atError()
.setMessage("aaaaa {}")
.log()</weak_warning>;
}
void foo2() {
Logger logger = LoggerFactory.getLogger(X.class);
<weak_warning descr="Similar log messages">logger.atError()
.setMessage("aaaaa 2{}")
.log()</weak_warning>;
@@ -575,5 +578,24 @@ class JavaLoggingSimilarMessageInspectionTest : LoggingSimilarMessageInspectionT
}
""".trimIndent())
}
fun `test sequence`() {
myFixture.testHighlighting(JvmLanguage.JAVA, """
import org.apache.logging.log4j.*;
class Logging {
private static final Logger LOG = LogManager.getLogger();
private static void request1(String i) {
LOG.info("testtesttest");
LOG.info("testtesttest");
if(LOG.isInfoEnabled()){
LOG.info("testtesttest");
}
if(LOG.isInfoEnabled()) LOG.info("testtesttest");
}
}
""".trimIndent())
}
}