Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow parentheses for unclear precedence with range operator #5143

Merged
merged 1 commit into from Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -99,8 +99,13 @@ class UnnecessaryParentheses(config: Config = Config.empty) : Rule(config) {
Precedence.SIMPLE_NAME, // (a to b) to c
),

// (a * b) + c
Precedence.MULTIPLICATIVE to arrayOf(Precedence.ADDITIVE),
Precedence.MULTIPLICATIVE to arrayOf(
Precedence.ADDITIVE, // (a * b) + c
Precedence.RANGE, // (a / b)..(c * d)
),

// (a + b)..(c + d)
Precedence.ADDITIVE to arrayOf(Precedence.RANGE),

// (a && b) || c
Precedence.CONJUNCTION to arrayOf(Precedence.DISJUNCTION),
Expand Down
Expand Up @@ -306,6 +306,19 @@ class UnnecessaryParenthesesSpec {
assertThat(testCase.rule.lint(code)).hasSize(if (testCase.allowForUnclearPrecedence) 0 else 4)
}

@ParameterizedTest
@MethodSource("cases")
fun `range operator when precedence is unclear`(testCase: RuleTestCase) {
val code = """
val a = (1 - 2)..(3 + 4)
val b = (1 / 2)..(3 * 4)
val c = (1 ?: 2)..(3 ?: 4) // parens required
val d = (1 to 2)..(3 to 4) // parens required
"""

assertThat(testCase.rule.lint(code)).hasSize(if (testCase.allowForUnclearPrecedence) 0 else 4)
}

@ParameterizedTest
@MethodSource("cases")
fun `multiple wrapping parentheses`(testCase: RuleTestCase) {
Expand Down