Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 21 additions & 48 deletions app/src/main/java/com/example/lint/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,44 +1,45 @@
package com.example.lint

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {

companion object {

private const val SSS = 2
}

const val SS_S_SFDAS = 2

val some = 2

fun spme () {
String ?.toString()
}
private const val EXTRA_PHOTO = "photo"

fun createIntent(context: Context, photo: String): Intent {
return Intent(context, MainActivity ::class.java).putExtra(EXTRA_PHOTO, photo)
}
fun someFunction() {}

fun createLauncher() {
createActivityLauncher()
fun createLauncher() = { someFunction() }
}

val SSs = 2

fun spme() {
String?.toString()

private fun createActivityLauncher(): String {
return "createLauncher()"
}

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)


val cTX: Context = this
String
.toString()
String::class.java


val list = listOf<String>()
Expand All @@ -48,56 +49,28 @@ class MainActivity : AppCompatActivity() {
}
}

private fun getTabName(position: Int): String {
return when (position) {
0 -> getString(R.string.app_name)
else -> getString(R.string.app_name)
}
private fun SSsome ( ) {
MainActivity .createIntent(this, "")
}



private fun SSsome(): Int {
val s = 2
val list = listOf("")
list.forEach { line ->
val some = line
}

val s3 = 1
return when (s) {
s3 -> { 0 }

0, 2 -> {
val some = 2
1
}
else -> 2
}

}



class SomeClass() {
val soURL = 2
}


fun emptyFun() {
fun emptyFun(): Int {
try {
val s = 2
} catch (e: Exception) {
throw e
}
}

object Auth : Screen()

open class Screen() {}
return when(1) {
1 -> 2
else -> 0
}

fun some(): String {
return "S"
}

protected val s2: String = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class LintIssueRegistry : IssueRegistry() {
override val issues: List<Issue>
get() = listOf(
NameFileUpperCamelCaseDetector.ISSUE,
AbbreviationDetector.ISSUE,// TODO need testing
AbbreviationDetector.ISSUE, // TODO need testing
PositionArgumentDetector.ISSUE,
MaxFunctionsArgumentsDetector.ISSUE,
ExceptionCatchDetector.ISSUE,
Expand All @@ -54,8 +54,8 @@ class LintIssueRegistry : IssueRegistry() {
SpaceMethodDetector.ISSUE,
NameFileSufixDetector.ISSUE,
AttributesPositionXmlDetector.ISSUE,
MaxClassInPackageDetector.ISSUE,// TODO need testing
MaxPackageCountDetector.ISSUE, // TODO need testing
MaxClassInPackageDetector.ISSUE, // TODO need testing
MaxPackageCountDetector.ISSUE, // TODO need testing
SimplificationsControlInstructionsDetector.ISSUE, // TODO need testing
IntentExtraParametersDetector.ISSUE,
ArgumentsBundleKeyPrefixDetector.ISSUE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,8 @@ class ArgumentsBundleKeyPrefixDetector : Detector(), Detector.UastScanner {
/** Issue describing the problem and pointing to the detector implementation */
@JvmField
val ISSUE: Issue = Issue.create(
// ID: used in @SuppressLint warnings etc
id = "OMEGA_USE_KEY_PREFIX_FOR_FRAGMENT_IN_ARGUMENTS_BUNDLE_PARAMS",
// Title -- shown in the IDE's preference dialog, as category headers in the
// Analysis results window, etc
briefDescription = "Use KEY prefix for Fragment in Arguments Bundle param.",
// Full explanation of the issue; you can use some markdown markup such as
// `monospace`, *italic*, and **bold**.
explanation = """
Use EXTRA prefix for intent arguments
http://wiki.omega-r.club/dev-android-code#rec228392168
Expand All @@ -36,32 +31,39 @@ class ArgumentsBundleKeyPrefixDetector : Detector(), Detector.UastScanner {
val FRAGMENT_REGEX = Regex("""Fragment$""")
}

override fun getApplicableUastTypes(): List<Class<out UElement?>>? {
return listOf(UCallExpression::class.java)
}
override fun getApplicableUastTypes(): List<Class<out UElement?>> = listOf(UCallExpression::class.java)

override fun createUastHandler(context: JavaContext): UElementHandler? {
override fun createUastHandler(context: JavaContext): UElementHandler {
return object : UElementHandler() {
override fun visitCallExpression(node: UCallExpression) {
val file = node.getContainingUFile() ?: return
val className = file.classes.firstOrNull()?.name ?: return
val name = node.methodName ?: return
if (className.contains(ArgumentsBundleKeyPrefixDetector.Companion.FRAGMENT_REGEX)) {
if (name.matches(ArgumentsBundleKeyPrefixDetector.Companion.PUT_PARCELABLE_METHOD_REGEX)) {
if (className.contains(FRAGMENT_REGEX)) {
if (name.matches(PUT_PARCELABLE_METHOD_REGEX)) {
val firstParam = node.valueArguments.firstOrNull() ?: return
val extraParam = firstParam.asRenderString()
if (!extraParam.contains(ArgumentsBundleKeyPrefixDetector.Companion.KEY_PREFIX_REGEX)) {
if (!extraParam.contains(KEY_PREFIX_REGEX)) {
context.report(
ArgumentsBundleKeyPrefixDetector.Companion.ISSUE,
ISSUE,
node,
context.getLocation(firstParam),
ArgumentsBundleKeyPrefixDetector.Companion.ISSUE.getExplanation(TextFormat.TEXT)
ISSUE.getExplanation(TextFormat.TEXT),
createFix(extraParam)
)
}
}
}
}
}
}

private fun createFix(extraParam: String): LintFix {
return fix()
.replace()
.text(extraParam)
.autoFix()
.build()
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,30 @@ class IntentExtraParametersDetector : Detector(), Detector.UastScanner {
/** Issue describing the problem and pointing to the detector implementation */
@JvmField
val ISSUE: Issue = Issue.create(
// ID: used in @SuppressLint warnings etc
id = "OMEGA_USE_EXTRA_PREFIX_FOR_INTENT_PARAMS",
briefDescription = "Use EXTRA prefix for intent arguments.",
explanation = """
// ID: used in @SuppressLint warnings etc
id = "OMEGA_USE_EXTRA_PREFIX_FOR_INTENT_PARAMS",
briefDescription = "Use EXTRA prefix for intent arguments.",
explanation = """
Use EXTRA prefix for intent arguments
http://wiki.omega-r.club/dev-android-code#rec228392168
""",
category = Category.CORRECTNESS,
priority = 7,
severity = Severity.WARNING,
implementation = Implementation(
IntentExtraParametersDetector::class.java,
Scope.JAVA_FILE_SCOPE
)
)
category = Category.CORRECTNESS,
priority = 7,
severity = Severity.WARNING,
implementation = Implementation(
IntentExtraParametersDetector::class.java,
Scope.JAVA_FILE_SCOPE
)
)

const val EXTRA_PREFIX_LABEL = "EXTRA_"
val EXTRA_PREFIX_REGEX = Regex("""^EXTRA_""")
val PUT_EXTRA_METHOD_REGEX = Regex("""^putExtra$""")
}

override fun getApplicableUastTypes(): List<Class<out UElement?>>? {
return listOf(UCallExpression::class.java)
}
override fun getApplicableUastTypes(): List<Class<out UElement?>> = listOf(UCallExpression::class.java)

override fun createUastHandler(context: JavaContext): UElementHandler? {
override fun createUastHandler(context: JavaContext): UElementHandler {
return object : UElementHandler() {
override fun visitCallExpression(node: UCallExpression) {
val name = node.methodName ?: return
Expand All @@ -43,11 +42,25 @@ class IntentExtraParametersDetector : Detector(), Detector.UastScanner {
val firstParam = node.valueArguments.firstOrNull() ?: return
val extraParam = firstParam.asRenderString()
if (!extraParam.contains(EXTRA_PREFIX_REGEX)) {
context.report(ISSUE, node, context.getLocation(firstParam), ISSUE.getExplanation(TextFormat.TEXT))
context.report(
ISSUE,
node,
context.getLocation(firstParam),
ISSUE.getExplanation(TextFormat.TEXT),
createFix(extraParam)
)
}
}
}
}
}

private fun createFix(extraParam: String): LintFix {
return LintFix.create()
.replace()
.text(extraParam)
.with("$EXTRA_PREFIX_LABEL$extraParam")
.build()
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ExceptionCatchDetector : Detector(), Detector.UastScanner {
explanation = """
Catch body is empty. Add exception handling.
http://wiki.omega-r.club/dev-android-code#rec226449864
""",
""",
category = Category.CORRECTNESS,
priority = 7,
severity = Severity.WARNING,
Expand All @@ -31,26 +31,27 @@ class ExceptionCatchDetector : Detector(), Detector.UastScanner {

private const val GENERALIZED_EXCEPTION_VAL = "java.lang.Exception"
private const val THROW_VAL = "throw"
private const val GENERALIZED_EXCEPTION_MESSAGE =
"Catch generalized exception. Should throw specific exception in catch body \n" +
"http://wiki.omega-r.club/dev-android-code#rec226454364"
private const val GENERALIZED_EXCEPTION_MESSAGE = """Catch generalized exception.
Should throw specific exception in catch body.
http://wiki.omega-r.club/dev-android-code#rec226454364
"""
}

override fun getApplicableUastTypes(): List<Class<out UElement?>>? {
return listOf(UCatchClause::class.java)
}
override fun getApplicableUastTypes(): List<Class<out UElement?>> = listOf(UCatchClause::class.java)

override fun createUastHandler(context: JavaContext): UElementHandler? {
override fun createUastHandler(context: JavaContext): UElementHandler {
return object : UElementHandler() {
override fun visitCatchClause(node: UCatchClause) {
val body = node.body
val string = body.asRenderString()

if (string.matches(EMPTY_BODY_REGEX)) {
context.report(
ISSUE,
body,
context.getNameLocation(body),
ISSUE.getExplanation(TextFormat.TEXT)
ISSUE.getExplanation(TextFormat.TEXT),
createEmptyBodyFix()
)
}
val parameters = node.parameters
Expand All @@ -60,8 +61,9 @@ class ExceptionCatchDetector : Detector(), Detector.UastScanner {
context.report(
ISSUE,
body,
context.getNameLocation(it),
GENERALIZED_EXCEPTION_MESSAGE
context.getLocation(it as UElement),
GENERALIZED_EXCEPTION_MESSAGE,
createEmptyBodyFix()
)
}
}
Expand All @@ -70,4 +72,12 @@ class ExceptionCatchDetector : Detector(), Detector.UastScanner {
}
}

private fun createEmptyBodyFix(): LintFix {
return LintFix.create()
.replace()
.text("}")
.with(" throw //something\n }")
.build()
}

}
Loading