Skip to content

Commit 2166328

Browse files
Merge pull request #36 from Omega-R/develop
Develop
2 parents 13eacf1 + e22b3ff commit 2166328

29 files changed

Lines changed: 510 additions & 427 deletions

File tree

app/src/main/java/com/example/lint/MainActivity.kt

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
11
package com.example.lint
22

3-
import android.annotation.SuppressLint
43
import android.content.Context
4+
import android.content.Intent
55
import android.os.Bundle
66
import androidx.appcompat.app.AppCompatActivity
77

8+
89
class MainActivity : AppCompatActivity() {
910

1011
companion object {
1112

12-
private const val SSS = 2
13-
}
14-
13+
const val SS_S_SFDAS = 2
1514

15+
val some = 2
1616

17-
fun spme () {
18-
String ?.toString()
19-
}
17+
private const val EXTRA_PHOTO = "photo"
2018

19+
fun createIntent(context: Context, photo: String): Intent {
20+
return Intent(context, MainActivity ::class.java).putExtra(EXTRA_PHOTO, photo)
21+
}
22+
fun someFunction() {}
2123

22-
fun createLauncher() {
23-
createActivityLauncher()
24+
fun createLauncher() = { someFunction() }
2425
}
2526

27+
val SSs = 2
28+
29+
fun spme() {
30+
String?.toString()
2631

27-
private fun createActivityLauncher(): String {
28-
return "createLauncher()"
2932
}
3033

3134
override fun onCreate(savedInstanceState: Bundle?) {
3235

33-
3436
super.onCreate(savedInstanceState)
3537
setContentView(R.layout.activity_main)
3638

3739

3840
val cTX: Context = this
3941
String
4042
.toString()
41-
String::class.java
4243

4344

4445
val list = listOf<String>()
@@ -48,56 +49,28 @@ class MainActivity : AppCompatActivity() {
4849
}
4950
}
5051

51-
private fun getTabName(position: Int): String {
52-
return when (position) {
53-
0 -> getString(R.string.app_name)
54-
else -> getString(R.string.app_name)
55-
}
52+
private fun SSsome ( ) {
53+
MainActivity .createIntent(this, "")
5654
}
5755

58-
59-
60-
private fun SSsome(): Int {
61-
val s = 2
62-
val list = listOf("")
63-
list.forEach { line ->
64-
val some = line
65-
}
66-
67-
val s3 = 1
68-
return when (s) {
69-
s3 -> { 0 }
70-
71-
0, 2 -> {
72-
val some = 2
73-
1
74-
}
75-
else -> 2
76-
}
77-
78-
}
79-
80-
81-
8256
class SomeClass() {
8357
val soURL = 2
8458
}
8559

8660

87-
fun emptyFun() {
61+
fun emptyFun(): Int {
8862
try {
8963
val s = 2
9064
} catch (e: Exception) {
9165
throw e
9266
}
93-
}
9467

95-
object Auth : Screen()
9668

97-
open class Screen() {}
69+
return when(1) {
70+
1 -> 2
71+
else -> 0
72+
}
9873

99-
fun some(): String {
100-
return "S"
10174
}
10275

10376
protected val s2: String = ""

checks/src/main/java/com/omegar/lint/checks/LintIssueRegistry.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class LintIssueRegistry : IssueRegistry() {
3535
override val issues: List<Issue>
3636
get() = listOf(
3737
NameFileUpperCamelCaseDetector.ISSUE,
38-
AbbreviationDetector.ISSUE,// TODO need testing
38+
AbbreviationDetector.ISSUE, // TODO need testing
3939
PositionArgumentDetector.ISSUE,
4040
MaxFunctionsArgumentsDetector.ISSUE,
4141
ExceptionCatchDetector.ISSUE,
@@ -54,8 +54,8 @@ class LintIssueRegistry : IssueRegistry() {
5454
SpaceMethodDetector.ISSUE,
5555
NameFileSufixDetector.ISSUE,
5656
AttributesPositionXmlDetector.ISSUE,
57-
MaxClassInPackageDetector.ISSUE,// TODO need testing
58-
MaxPackageCountDetector.ISSUE, // TODO need testing
57+
MaxClassInPackageDetector.ISSUE, // TODO need testing
58+
MaxPackageCountDetector.ISSUE, // TODO need testing
5959
SimplificationsControlInstructionsDetector.ISSUE, // TODO need testing
6060
IntentExtraParametersDetector.ISSUE,
6161
ArgumentsBundleKeyPrefixDetector.ISSUE,

checks/src/main/java/com/omegar/lint/checks/detector/code_guidelines/general_recommendations/parameter_passing/argument_bundle_for_fragments_creation/ArgumentsBundleKeyPrefixDetector.kt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@ class ArgumentsBundleKeyPrefixDetector : Detector(), Detector.UastScanner {
1111
/** Issue describing the problem and pointing to the detector implementation */
1212
@JvmField
1313
val ISSUE: Issue = Issue.create(
14-
// ID: used in @SuppressLint warnings etc
1514
id = "OMEGA_USE_KEY_PREFIX_FOR_FRAGMENT_IN_ARGUMENTS_BUNDLE_PARAMS",
16-
// Title -- shown in the IDE's preference dialog, as category headers in the
17-
// Analysis results window, etc
1815
briefDescription = "Use KEY prefix for Fragment in Arguments Bundle param.",
19-
// Full explanation of the issue; you can use some markdown markup such as
20-
// `monospace`, *italic*, and **bold**.
2116
explanation = """
2217
Use EXTRA prefix for intent arguments
2318
http://wiki.omega-r.club/dev-android-code#rec228392168
@@ -36,32 +31,39 @@ class ArgumentsBundleKeyPrefixDetector : Detector(), Detector.UastScanner {
3631
val FRAGMENT_REGEX = Regex("""Fragment$""")
3732
}
3833

39-
override fun getApplicableUastTypes(): List<Class<out UElement?>>? {
40-
return listOf(UCallExpression::class.java)
41-
}
34+
override fun getApplicableUastTypes(): List<Class<out UElement?>> = listOf(UCallExpression::class.java)
4235

43-
override fun createUastHandler(context: JavaContext): UElementHandler? {
36+
override fun createUastHandler(context: JavaContext): UElementHandler {
4437
return object : UElementHandler() {
4538
override fun visitCallExpression(node: UCallExpression) {
4639
val file = node.getContainingUFile() ?: return
4740
val className = file.classes.firstOrNull()?.name ?: return
4841
val name = node.methodName ?: return
49-
if (className.contains(ArgumentsBundleKeyPrefixDetector.Companion.FRAGMENT_REGEX)) {
50-
if (name.matches(ArgumentsBundleKeyPrefixDetector.Companion.PUT_PARCELABLE_METHOD_REGEX)) {
42+
if (className.contains(FRAGMENT_REGEX)) {
43+
if (name.matches(PUT_PARCELABLE_METHOD_REGEX)) {
5144
val firstParam = node.valueArguments.firstOrNull() ?: return
5245
val extraParam = firstParam.asRenderString()
53-
if (!extraParam.contains(ArgumentsBundleKeyPrefixDetector.Companion.KEY_PREFIX_REGEX)) {
46+
if (!extraParam.contains(KEY_PREFIX_REGEX)) {
5447
context.report(
55-
ArgumentsBundleKeyPrefixDetector.Companion.ISSUE,
48+
ISSUE,
5649
node,
5750
context.getLocation(firstParam),
58-
ArgumentsBundleKeyPrefixDetector.Companion.ISSUE.getExplanation(TextFormat.TEXT)
51+
ISSUE.getExplanation(TextFormat.TEXT),
52+
createFix(extraParam)
5953
)
6054
}
6155
}
6256
}
6357
}
6458
}
6559
}
60+
61+
private fun createFix(extraParam: String): LintFix {
62+
return fix()
63+
.replace()
64+
.text(extraParam)
65+
.autoFix()
66+
.build()
67+
}
6668
}
6769

checks/src/main/java/com/omegar/lint/checks/detector/code_guidelines/general_recommendations/parameter_passing/intent_creation/IntentExtraParametersDetector.kt

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,30 @@ class IntentExtraParametersDetector : Detector(), Detector.UastScanner {
1010
/** Issue describing the problem and pointing to the detector implementation */
1111
@JvmField
1212
val ISSUE: Issue = Issue.create(
13-
// ID: used in @SuppressLint warnings etc
14-
id = "OMEGA_USE_EXTRA_PREFIX_FOR_INTENT_PARAMS",
15-
briefDescription = "Use EXTRA prefix for intent arguments.",
16-
explanation = """
13+
// ID: used in @SuppressLint warnings etc
14+
id = "OMEGA_USE_EXTRA_PREFIX_FOR_INTENT_PARAMS",
15+
briefDescription = "Use EXTRA prefix for intent arguments.",
16+
explanation = """
1717
Use EXTRA prefix for intent arguments
1818
http://wiki.omega-r.club/dev-android-code#rec228392168
1919
""",
20-
category = Category.CORRECTNESS,
21-
priority = 7,
22-
severity = Severity.WARNING,
23-
implementation = Implementation(
24-
IntentExtraParametersDetector::class.java,
25-
Scope.JAVA_FILE_SCOPE
26-
)
27-
)
20+
category = Category.CORRECTNESS,
21+
priority = 7,
22+
severity = Severity.WARNING,
23+
implementation = Implementation(
24+
IntentExtraParametersDetector::class.java,
25+
Scope.JAVA_FILE_SCOPE
26+
)
27+
)
2828

29+
const val EXTRA_PREFIX_LABEL = "EXTRA_"
2930
val EXTRA_PREFIX_REGEX = Regex("""^EXTRA_""")
3031
val PUT_EXTRA_METHOD_REGEX = Regex("""^putExtra$""")
3132
}
3233

33-
override fun getApplicableUastTypes(): List<Class<out UElement?>>? {
34-
return listOf(UCallExpression::class.java)
35-
}
34+
override fun getApplicableUastTypes(): List<Class<out UElement?>> = listOf(UCallExpression::class.java)
3635

37-
override fun createUastHandler(context: JavaContext): UElementHandler? {
36+
override fun createUastHandler(context: JavaContext): UElementHandler {
3837
return object : UElementHandler() {
3938
override fun visitCallExpression(node: UCallExpression) {
4039
val name = node.methodName ?: return
@@ -43,11 +42,25 @@ class IntentExtraParametersDetector : Detector(), Detector.UastScanner {
4342
val firstParam = node.valueArguments.firstOrNull() ?: return
4443
val extraParam = firstParam.asRenderString()
4544
if (!extraParam.contains(EXTRA_PREFIX_REGEX)) {
46-
context.report(ISSUE, node, context.getLocation(firstParam), ISSUE.getExplanation(TextFormat.TEXT))
45+
context.report(
46+
ISSUE,
47+
node,
48+
context.getLocation(firstParam),
49+
ISSUE.getExplanation(TextFormat.TEXT),
50+
createFix(extraParam)
51+
)
4752
}
4853
}
4954
}
5055
}
5156
}
57+
58+
private fun createFix(extraParam: String): LintFix {
59+
return LintFix.create()
60+
.replace()
61+
.text(extraParam)
62+
.with("$EXTRA_PREFIX_LABEL$extraParam")
63+
.build()
64+
}
5265
}
5366

checks/src/main/java/com/omegar/lint/checks/detector/code_guidelines/kotlin_rules/exception/ExceptionCatchDetector.kt

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ExceptionCatchDetector : Detector(), Detector.UastScanner {
1717
explanation = """
1818
Catch body is empty. Add exception handling.
1919
http://wiki.omega-r.club/dev-android-code#rec226449864
20-
""",
20+
""",
2121
category = Category.CORRECTNESS,
2222
priority = 7,
2323
severity = Severity.WARNING,
@@ -31,26 +31,27 @@ class ExceptionCatchDetector : Detector(), Detector.UastScanner {
3131

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

39-
override fun getApplicableUastTypes(): List<Class<out UElement?>>? {
40-
return listOf(UCatchClause::class.java)
41-
}
40+
override fun getApplicableUastTypes(): List<Class<out UElement?>> = listOf(UCatchClause::class.java)
4241

43-
override fun createUastHandler(context: JavaContext): UElementHandler? {
42+
override fun createUastHandler(context: JavaContext): UElementHandler {
4443
return object : UElementHandler() {
4544
override fun visitCatchClause(node: UCatchClause) {
4645
val body = node.body
4746
val string = body.asRenderString()
47+
4848
if (string.matches(EMPTY_BODY_REGEX)) {
4949
context.report(
5050
ISSUE,
5151
body,
5252
context.getNameLocation(body),
53-
ISSUE.getExplanation(TextFormat.TEXT)
53+
ISSUE.getExplanation(TextFormat.TEXT),
54+
createEmptyBodyFix()
5455
)
5556
}
5657
val parameters = node.parameters
@@ -60,8 +61,9 @@ class ExceptionCatchDetector : Detector(), Detector.UastScanner {
6061
context.report(
6162
ISSUE,
6263
body,
63-
context.getNameLocation(it),
64-
GENERALIZED_EXCEPTION_MESSAGE
64+
context.getLocation(it as UElement),
65+
GENERALIZED_EXCEPTION_MESSAGE,
66+
createEmptyBodyFix()
6567
)
6668
}
6769
}
@@ -70,4 +72,12 @@ class ExceptionCatchDetector : Detector(), Detector.UastScanner {
7072
}
7173
}
7274

75+
private fun createEmptyBodyFix(): LintFix {
76+
return LintFix.create()
77+
.replace()
78+
.text("}")
79+
.with(" throw //something\n }")
80+
.build()
81+
}
82+
7383
}

0 commit comments

Comments
 (0)