Skip to content

Commit

Permalink
Fix Lint fallthrough errors
Browse files Browse the repository at this point in the history
Closes #434
  • Loading branch information
John Rodriguez committed Aug 12, 2021
1 parent 880a2e6 commit 21628df
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,8 @@ class WrongTimberUsageDetector : Detector(), UastScanner {
checkNestedStringFormat(context, node)
return
}
// As of API 26, Log tags are no longer limited to 23 chars.
if ("tag" == methodName
&& evaluator.isMemberInClass(method, "timber.log.Timber")
&& context.project.minSdk < 26
) {
checkTagLength(context, node)
return
if ("tag" == methodName && evaluator.isMemberInClass(method, "timber.log.Timber")) {
checkTagLengthIfMinSdkLessThan26(context, node)
}
if (evaluator.isMemberInClass(method, "android.util.Log")) {
context.report(
Expand Down Expand Up @@ -144,7 +139,7 @@ class WrongTimberUsageDetector : Detector(), UastScanner {
}
}

private fun checkTagLength(context: JavaContext, call: UCallExpression) {
private fun checkTagLengthIfMinSdkLessThan26(context: JavaContext, call: UCallExpression) {
val argument = call.valueArguments[0]
val tag = evaluateString(context, argument, true)
if (tag != null && tag.length > 23) {
Expand All @@ -156,6 +151,7 @@ class WrongTimberUsageDetector : Detector(), UastScanner {
message = "The logging tag can be at most 23 characters, was ${tag.length} ($tag)",
fix = quickFixIssueTagLength(argument, tag)
),
// As of API 26, Log tags are no longer limited to 23 chars.
constraint = minSdkLessThan(26)
)
}
Expand All @@ -178,7 +174,7 @@ class WrongTimberUsageDetector : Detector(), UastScanner {
startIndexOfArguments++
}

val formatString = evaluateString(context, formatStringArg, true)
val formatString = evaluateString(context, formatStringArg, false)
?: return // We passed for example a method call

val formatArgumentCount = getFormatArgumentCount(formatString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,33 @@ class WrongTimberUsageDetectorTest {
.expectClean()
}

@Test fun validStringFormatExtracted() {
lint()
.files(TIMBER_STUB,
java("""
|package foo;
|import timber.log.Timber;
|public class Example {
| public void log() {
| String message = String.format("%s", "foo");
| Timber.d(message);
| }
|}""".trimMargin()),
kotlin("""
|package foo
|import timber.log.Timber
|class Example {
| fun log() {
| val message = String.format("%s", "foo")
| Timber.d(message)
| }
|}""".trimMargin()),
)
.issues(*issues)
.run()
.expectClean()
}

@Test fun throwableNotAtBeginning() {
lint()
.files(TIMBER_STUB,
Expand Down Expand Up @@ -774,38 +801,6 @@ class WrongTimberUsageDetectorTest {
|1 errors, 0 warnings""".trimMargin())
}

@Test fun tagTooLongLiteralPlusField() {
lint()
.files(TIMBER_STUB,
java("""
|package foo;
|import timber.log.Timber;
|public class Example {
| private final String field = "x";
| public void log() {
| Timber.tag("abcdefghijklmnopqrstuvw" + field);
| }
|}""".trimMargin()),
kotlin("""
|package foo
|import timber.log.Timber
|class Example {
| private val field = "x"
| fun log() {
| Timber.tag("abcdefghijklmnopqrstuvw${"$"}field")
| }
|}""".trimMargin()),
manifest().minSdk(25)
)
.issues(*issues)
.run()
.expect("""
|src/foo/Example.java:6: Error: The logging tag can be at most 23 characters, was 24 (abcdefghijklmnopqrstuvwx) [TimberTagLength]
| Timber.tag("abcdefghijklmnopqrstuvw" + field);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|1 errors, 0 warnings""".trimMargin())
}

@Test fun tagTooLongLiteralOnlyBeforeApi26() {
lint()
.files(TIMBER_STUB,
Expand All @@ -832,34 +827,6 @@ class WrongTimberUsageDetectorTest {
.expectClean()
}

@Test fun tagTooLongLiteralPlusFieldOnlyBeforeApi26() {
lint()
.files(TIMBER_STUB,
java("""
|package foo;
|import timber.log.Timber;
|public class Example {
| private final String field = "x";
| public void log() {
| Timber.tag("abcdefghijklmnopqrstuvw" + field);
| }
|}""".trimMargin()),
kotlin("""
|package foo
|import timber.log.Timber
|class Example {
| private val field = "x"
| fun log() {
| Timber.tag("abcdefghijklmnopqrstuvw${"$"}field")
| }
|}""".trimMargin()),
manifest().minSdk(26)
)
.issues(*issues)
.run()
.expectClean()
}

@Test fun tooManyFormatArgsInTag() {
lint()
.files(TIMBER_STUB,
Expand Down

0 comments on commit 21628df

Please sign in to comment.