Skip to content

Commit

Permalink
Merge pull request #19 from wordpress-mobile/fix/ignore-enums-in-null…
Browse files Browse the repository at this point in the history
…annotation-detector

Ignore enums in null annotation detector
  • Loading branch information
mkevins authored Mar 13, 2024
2 parents e668b09 + 13b02ce commit b374448
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Pluggable lint module for WordPress-Android.
* In your build.gradle:
```groovy
dependencies {
lintChecks 'org.wordpress:lint:2.0.0' // use version 2.0.0
lintChecks 'org.wordpress:lint:2.1.0' // use version 2.1.0
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ import com.android.tools.lint.detector.api.Severity
import com.android.tools.lint.detector.api.SourceCodeScanner
import com.android.tools.lint.detector.api.isJava
import com.intellij.psi.PsiAnnotation
import com.intellij.psi.PsiEnumConstant
import com.intellij.psi.PsiPrimitiveType
import org.jetbrains.uast.UAnnotated
import org.jetbrains.uast.UAnnotationMethod
import org.jetbrains.uast.UAnonymousClass
import org.jetbrains.uast.UElement
import org.jetbrains.uast.UEnumConstant
import org.jetbrains.uast.UField
import org.jetbrains.uast.UMethod
import org.jetbrains.uast.UParameter
import org.jetbrains.uast.UVariable
import org.jetbrains.uast.getContainingUClass
import org.jetbrains.uast.getContainingUMethod

class MissingNullAnnotationDetector : Detector(), SourceCodeScanner {
override fun getApplicableUastTypes(): List<Class<out UElement>> = listOf(
Expand Down Expand Up @@ -101,7 +102,7 @@ class MissingNullAnnotationDetector : Detector(), SourceCodeScanner {
private val UVariable.isPrimitive
get() = type is PsiPrimitiveType
private val UVariable.isEnum
get() = this is UEnumConstant
get() = this is PsiEnumConstant
private val UVariable.isInjected
get() = annotations.hasInject
private val UVariable.isConstant
Expand All @@ -111,15 +112,23 @@ private val UVariable.isInitializedFinalField
private val UVariable.requiresNullAnnotation
get() = !(isPrimitive || isEnum || isConstant || isInitializedFinalField || isInjected)

/* UParameter Extensions */
private val UParameter.requiresNullAnnotation
get() = this.getContainingUMethod()?.uastBody != null
&& (this as UVariable).requiresNullAnnotation

/* UMethod Extensions */
private val UMethod.isPrimitive
get() = returnType is PsiPrimitiveType
private val UMethod.requiresNullAnnotation
get() = this !is UAnnotationMethod && !isPrimitive && !isConstructor
get() = this !is UAnnotationMethod && uastBody != null && !isPrimitive && !isConstructor
&& !isEnum
private val UMethod.isAnonymousConstructor
get() = isConstructor && getContainingUClass()?.let { it is UAnonymousClass } == true
private val UMethod.isInjected
get() = annotations.hasInject
private val UMethod.isEnum
get() = returnType is PsiEnumConstant

/* UAnnotated Extensions */
private val UAnnotated.isNullAnnotated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,24 @@ class MissingNullAnnotationDetectorTest {
.run()
.expectClean()
}
@Test
fun `it ignores enums`() {
lint().files(LintDetectorTest.java("""
package test;
public enum TrafficLightState {
RED,
YELLOW,
GREEN
}
""").indented())
.issues(
MissingNullAnnotationDetector.MISSING_FIELD_ANNOTATION,
MissingNullAnnotationDetector.MISSING_CONSTRUCTOR_PARAMETER_ANNOTATION,
MissingNullAnnotationDetector.MISSING_METHOD_PARAMETER_ANNOTATION,
MissingNullAnnotationDetector.MISSING_METHOD_RETURN_TYPE_ANNOTATION
)
.run()
.expectClean()
}
}

0 comments on commit b374448

Please sign in to comment.