You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The fixBooleanExpression method in BooleanExpressionsRule can generate incorrect code.
Any boolean logic is represented there as a String with each of the elemental subexpressions as a single uppercase letter.
Example: A & G & !D & (!B | !C | !H)
The problem arises when any of the already replaced expressions contains one of the yet to replace characters.
Example: A & B & !C
A --> isBold()
B --> foo
C --> bar
First A is replaced: isBold() & B & !C
When now B is replaced, actually all instanced from the beginning of the whole expression String are replaced: isfooold() & foo & C
and so on ...
Expected behavior
Correct code is generated for arbitrarily complex boolean expressions
Observed behavior
Exception KotlinParseException("Text is not valid: [...]") when the incorrectly generated code is parsed.
Usually this results in invalid code that can not be parsed, but surely an example can be crafted that goes unnoticed, potentially permanently changing the code logic (which easily slips review when relying on the correct behaviour of a code formatting tool)
Environment information
diktat version: 1.1.0
build tool (maven/gradle): gradle
how is diktat run (CLI, plugin, etc.): spotless
kotlin version: 1.6.20
operating system: windows/ubuntu
The text was updated successfully, but these errors were encountered:
@HaukeRa thank you for this report! Boolean logic in diktat is quite complex, so we will need some time to investigate it… right now you can disable this inspection using Suppress annotation
Describe the bug
The
fixBooleanExpression
method inBooleanExpressionsRule
can generate incorrect code.Any boolean logic is represented there as a String with each of the elemental subexpressions as a single uppercase letter.
Example:
A & G & !D & (!B | !C | !H)
As a last step, the placeholder letters are replaced with the actual expressions:
https://github.com/analysis-dev/diktat/blob/90b852939fb5b606e96611004435c8038e991982/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/BooleanExpressionsRule.kt#L170
The problem arises when any of the already replaced expressions contains one of the yet to replace characters.
Example:
A & B & !C
A --> isBold()
B --> foo
C --> bar
First
A
is replaced:isBold() & B & !C
When now B is replaced, actually all instanced from the beginning of the whole expression String are replaced:
isfooold() & foo & C
and so on ...
Expected behavior
Correct code is generated for arbitrarily complex boolean expressions
Observed behavior
Exception
KotlinParseException("Text is not valid: [...]")
when the incorrectly generated code is parsed.Usually this results in invalid code that can not be parsed, but surely an example can be crafted that goes unnoticed, potentially permanently changing the code logic (which easily slips review when relying on the correct behaviour of a code formatting tool)
Environment information
The text was updated successfully, but these errors were encountered: