Skip to content

Commit

Permalink
Fix false alert `Format was not able to resolve all violations which …
Browse files Browse the repository at this point in the history
…(theoretically) can be autocorrected` (#2727)

The warning "Format was not able to resolve all violations" should not be logged in case the code only contains lint violations which may not be autocorrected according to the AutoCorrectHandler.

Closes #2726
  • Loading branch information
paul-dingemans committed Jul 2, 2024
1 parent 52534ca commit 15fb36d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class BaselineCLITest {
projectName,
listOf(
"--baseline=$baselinePath",
"--format",
"TestBaselineFile.kt.test",
"some/path/to/TestBaselineFile.kt.test",
),
Expand All @@ -66,6 +67,10 @@ class BaselineCLITest {
"For those references the rule set id 'standard' is assumed. It is advised to regenerate " +
"this baseline file.*",
),
).doesNotContainLineMatching(
Regex(
".*Format was not able to resolve all violations which \\(theoretically\\) can be autocorrected in file.*",
),
)
}.assertAll()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ internal class CodeFormatter(
mutated =
format(autocorrectHandler, code).let { ruleErrors ->
errors.addAll(ruleErrors)
ruleErrors.any { it.first.canBeAutoCorrected }
// Check if at least one error can be autocorrected, and fixing this error has been approved by the autocorrect
// handler
ruleErrors.any { it.first.canBeAutoCorrected && it.second }
}
formatRunCount++
} while (mutated && formatRunCount < maxFormatRunsPerFile)
Expand Down Expand Up @@ -116,10 +118,6 @@ internal class CodeFormatter(
executeRule(rule, autocorrectHandler) { offset, errorMessage, canBeAutoCorrected ->
val (line, col) = positionInTextLocator(offset)
val lintError = LintError(line, col, rule.ruleId, errorMessage, canBeAutoCorrected)
// In trace mode report the violation immediately. The order in which violations are actually found might be
// different from the order in which they are reported. For debugging purposes it can be helpful to know the
// exact order in which violations are being solved.
LOGGER.trace { "Format violation: ${lintError.logMessage(code)}" }

// Always request the autocorrectDecision, even in case it is already known that the LintError can not be autocorrected. In
// this way the API Consumer can still use data from the LintError for other purposes.
Expand All @@ -128,6 +126,12 @@ internal class CodeFormatter(
.also { autocorrectDecision ->
// Ignore decision of the API Consumer in case the error can not be autocorrected
val autocorrect = autocorrectDecision == ALLOW_AUTOCORRECT && canBeAutoCorrected
if (autocorrect) {
// In trace mode report the violation immediately. The order in which violations are actually found might be
// different from the order in which they are reported. For debugging purposes it can be helpful to know the
// exact order in which violations are being solved.
LOGGER.trace { "Format violation: ${lintError.logMessage(code)}" }
}
errors.add(
Pair(
lintError,
Expand Down

0 comments on commit 15fb36d

Please sign in to comment.