Skip to content

Commit

Permalink
Allow string literal concatenation
Browse files Browse the repository at this point in the history
Do not report a lint for a binary operation when the operands are both
string literals. This is to allow long log lines to be broken into
multiple lines of code.
  • Loading branch information
camsteffen committed Dec 10, 2019
1 parent f6d1b98 commit a632a7b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,10 @@ private boolean checkElement(JavaContext context, UCallExpression call, UElement
if (operator == PLUS || operator == PLUS_ASSIGN) {
Class argumentType = getType(binaryExpression);
if (argumentType == String.class) {
if (isStringLiteral(binaryExpression.getLeftOperand())
&& isStringLiteral(binaryExpression.getRightOperand())) {
return false;
}
LintFix fix = quickFixIssueBinary(binaryExpression);
context.report(ISSUE_BINARY, call, context.getLocation(element),
"Replace String concatenation with Timber's string formatting", fix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,17 +333,7 @@ class WrongTimberUsageDetectorTest {
|}""".trimMargin()))
.issues(WrongTimberUsageDetector.ISSUE_BINARY)
.run()
.expect("""
|src/foo/Example.java:5: Warning: Replace String concatenation with Timber's string formatting [BinaryOperationInTimber]
| Timber.d("foo" + "bar");
| ~~~~~~~~~~~~~
|0 errors, 1 warnings""".trimMargin())
.expectFixDiffs("""
|Fix for src/foo/Example.java line 4: Replace with "foobar":
|@@ -5 +5
|- Timber.d("foo" + "bar");
|+ Timber.d("foobar");
|""".trimMargin())
.expectClean()
}

@Test fun stringConcatenationLeftLiteral() {
Expand Down

0 comments on commit a632a7b

Please sign in to comment.