Skip to content

Commit 4600b6d

Browse files
committed
Check inspection settings before adding and reporting on unsafe = true
1 parent 3412b35 commit 4600b6d

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

src/main/kotlin/platform/mixin/handlers/injectionPoint/CtorHeadInjectionPoint.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020

2121
package com.demonwav.mcdev.platform.mixin.handlers.injectionPoint
2222

23+
import com.demonwav.mcdev.platform.mixin.inspection.injector.CtorHeadNoUnsafeInspection
2324
import com.demonwav.mcdev.platform.mixin.reference.MixinSelector
2425
import com.demonwav.mcdev.platform.mixin.util.findOrConstructSourceMethod
2526
import com.demonwav.mcdev.platform.mixin.util.findSuperConstructorCall
2627
import com.demonwav.mcdev.platform.mixin.util.isConstructor
2728
import com.demonwav.mcdev.util.createLiteralExpression
2829
import com.demonwav.mcdev.util.enumValueOfOrNull
2930
import com.demonwav.mcdev.util.findContainingClass
31+
import com.demonwav.mcdev.util.findInspection
3032
import com.intellij.codeInsight.lookup.LookupElementBuilder
3133
import com.intellij.openapi.editor.Editor
3234
import com.intellij.openapi.project.Project
@@ -53,8 +55,16 @@ import org.objectweb.asm.tree.MethodNode
5355

5456
class CtorHeadInjectionPoint : InjectionPoint<PsiElement>() {
5557
override fun onCompleted(editor: Editor, reference: PsiLiteral) {
56-
val at = reference.parentOfType<PsiAnnotation>() ?: return
5758
val project = reference.project
59+
60+
// avoid adding unsafe = true when it's unnecessary on Fabric
61+
val noUnsafeInspection =
62+
project.findInspection<CtorHeadNoUnsafeInspection>(CtorHeadNoUnsafeInspection.SHORT_NAME)
63+
if (noUnsafeInspection?.ignoreForFabric == true) {
64+
return
65+
}
66+
67+
val at = reference.parentOfType<PsiAnnotation>() ?: return
5868
at.setDeclaredAttributeValue(
5969
"unsafe",
6070
JavaPsiFacade.getElementFactory(project).createLiteralExpression(true)

src/main/kotlin/platform/mixin/inspection/injector/CtorHeadNoUnsafeInspection.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,8 @@ class CtorHeadNoUnsafeInspection : MixinInspection() {
8282
}
8383
}
8484
}
85+
86+
companion object {
87+
const val SHORT_NAME = "CtorHeadNoUnsafe"
88+
}
8589
}

src/main/kotlin/platform/mixin/inspection/injector/UnnecessaryUnsafeInspection.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import com.demonwav.mcdev.platform.mixin.util.MethodTargetMember
2929
import com.demonwav.mcdev.platform.mixin.util.MixinConstants
3030
import com.demonwav.mcdev.platform.mixin.util.isConstructor
3131
import com.demonwav.mcdev.util.constantValue
32+
import com.demonwav.mcdev.util.findInspection
3233
import com.demonwav.mcdev.util.findModule
3334
import com.demonwav.mcdev.util.ifEmpty
3435
import com.intellij.codeInspection.ProblemsHolder
@@ -64,13 +65,16 @@ class UnnecessaryUnsafeInspection : MixinInspection() {
6465
val isFabric = holder.file.findModule()?.let { MinecraftFacet.getInstance(it) }?.isOfType(FabricModuleType)
6566
?: false
6667
val alwaysUnnecessary = isFabric && alwaysUnnecessaryOnFabric
68+
val requiresUnsafeForCtorHeadOnFabric =
69+
holder.project.findInspection<CtorHeadNoUnsafeInspection>(CtorHeadNoUnsafeInspection.SHORT_NAME)
70+
?.ignoreForFabric == false
6771

6872
return object : JavaElementVisitor() {
6973
override fun visitAnnotation(annotation: PsiAnnotation) {
7074
if (!annotation.hasQualifiedName(MixinConstants.Annotations.AT)) {
7175
return
7276
}
73-
if (!alwaysUnnecessary &&
77+
if ((!alwaysUnnecessary || requiresUnsafeForCtorHeadOnFabric) &&
7478
annotation.findDeclaredAttributeValue("value")?.constantValue == "CTOR_HEAD"
7579
) {
7680
// this case is handled by a specific inspection for CTOR_HEAD
@@ -94,6 +98,8 @@ class UnnecessaryUnsafeInspection : MixinInspection() {
9498
}
9599

96100
companion object {
101+
const val SHORT_NAME = "UnnecessaryUnsafe"
102+
97103
fun mightTargetConstructor(project: Project, at: PsiAnnotation): Boolean {
98104
val injectorAnnotation = at.parents(false)
99105
.takeWhile { it !is PsiClass }

src/main/kotlin/util/utils.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package com.demonwav.mcdev.util
2222

2323
import com.google.gson.Gson
2424
import com.google.gson.reflect.TypeToken
25+
import com.intellij.codeInspection.InspectionProfileEntry
2526
import com.intellij.lang.java.lexer.JavaLexer
2627
import com.intellij.openapi.application.AppUIExecutor
2728
import com.intellij.openapi.application.ApplicationManager
@@ -42,12 +43,14 @@ import com.intellij.openapi.util.Condition
4243
import com.intellij.openapi.util.Ref
4344
import com.intellij.openapi.util.text.StringUtil
4445
import com.intellij.pom.java.LanguageLevel
46+
import com.intellij.profile.codeInspection.InspectionProfileManager
4547
import com.intellij.psi.PsiDocumentManager
4648
import com.intellij.psi.PsiFile
4749
import java.lang.invoke.MethodHandles
4850
import java.util.Locale
4951
import kotlin.math.min
5052
import kotlin.reflect.KClass
53+
import org.jetbrains.annotations.NonNls
5154
import org.jetbrains.concurrency.Promise
5255
import org.jetbrains.concurrency.runAsync
5356

@@ -131,6 +134,11 @@ fun waitForAllSmart() {
131134
}
132135
}
133136

137+
inline fun <reified T : InspectionProfileEntry> Project.findInspection(@NonNls shortName: String): T? =
138+
InspectionProfileManager.getInstance(this)
139+
.currentProfile.getInspectionTool(shortName, this)
140+
?.tool as? T
141+
134142
/**
135143
* Returns an untyped array for the specified [Collection].
136144
*/

0 commit comments

Comments
 (0)