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

Report once for method or constructor parameters that has no null annotation #20

Draft
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,19 @@ class MissingNullAnnotationDetector : Detector(), SourceCodeScanner {
}

if (!node.isInjected) {
node.uastParameters.forEach { visitParameter(node, it) }
if (node.uastParameters.any { it.requiresNullAnnotation && !it.isNullAnnotated }) {
if (node.isConstructor) {
report(node, MISSING_CONSTRUCTOR_PARAMETER_ANNOTATION)
} else {
report(node, MISSING_METHOD_PARAMETER_ANNOTATION)
}
}
}

if (node.requiresNullAnnotation && !node.isNullAnnotated) {
report(node, MISSING_METHOD_RETURN_TYPE_ANNOTATION)
}
}

private fun visitParameter(node: UMethod, parameter: UParameter) {
if (parameter.requiresNullAnnotation && !parameter.isNullAnnotated) {
if (node.isConstructor) {
report(parameter, MISSING_CONSTRUCTOR_PARAMETER_ANNOTATION)
} else {
report(parameter, MISSING_METHOD_PARAMETER_ANNOTATION)
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.wordpress.android.lint

import com.android.tools.lint.checks.infrastructure.LintDetectorTest
import com.android.tools.lint.checks.infrastructure.TestLintTask.lint
import com.android.tools.lint.detector.api.Severity
import org.junit.Test
import org.wordpress.android.lint.Utils.nonNullClass
import org.wordpress.android.lint.Utils.nullableClass
Expand Down Expand Up @@ -148,17 +149,18 @@ class MissingNullAnnotationDetectorTest {
package test;

class ExampleClass {
String getMessage(String name) {
String getMessage(String name, String title) {
return name + " example";
}
}
""").indented())
.issues(MissingNullAnnotationDetector.MISSING_METHOD_PARAMETER_ANNOTATION)
.run()
.expectCount(1, Severity.INFORMATIONAL)
.expect("""
src/test/ExampleClass.java:4: Information: Missing null annotation [MissingNullAnnotationOnMethodParameter]
String getMessage(String name) {
~~~~~~~~~~~
String getMessage(String name, String title) {
~~~~~~~~~~
0 errors, 0 warnings
"""
.trimIndent()
Expand Down Expand Up @@ -210,15 +212,16 @@ class MissingNullAnnotationDetectorTest {
package test;

class ExampleClass {
ExampleClass(String name) {}
ExampleClass(String name, String title) {}
}
""").indented())
.issues(MissingNullAnnotationDetector.MISSING_CONSTRUCTOR_PARAMETER_ANNOTATION)
.run()
.expectCount(1, Severity.INFORMATIONAL)
.expect("""
src/test/ExampleClass.java:4: Information: Missing null annotation [MissingNullAnnotationOnConstructorParameter]
ExampleClass(String name) {}
~~~~~~~~~~~
ExampleClass(String name, String title) {}
~~~~~~~~~~~~
0 errors, 0 warnings
"""
.trimIndent()
Expand Down