Skip to content

Commit ab98549

Browse files
committed
Add inspection for CTOR_HEAD missing unsafe = true
1 parent 53c3cca commit ab98549

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Minecraft Development for IntelliJ
3+
*
4+
* https://mcdev.io/
5+
*
6+
* Copyright (C) 2024 minecraft-dev
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published
10+
* by the Free Software Foundation, version 3.0 only.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
package com.demonwav.mcdev.platform.mixin.inspection.injector
22+
23+
import com.demonwav.mcdev.facet.MinecraftFacet
24+
import com.demonwav.mcdev.platform.fabric.FabricModuleType
25+
import com.demonwav.mcdev.platform.mixin.inspection.MixinInspection
26+
import com.demonwav.mcdev.platform.mixin.util.MixinConstants
27+
import com.demonwav.mcdev.util.constantStringValue
28+
import com.demonwav.mcdev.util.constantValue
29+
import com.demonwav.mcdev.util.findModule
30+
import com.intellij.codeInspection.ProblemsHolder
31+
import com.intellij.psi.JavaElementVisitor
32+
import com.intellij.psi.PsiAnnotation
33+
import com.intellij.psi.PsiElementVisitor
34+
import java.awt.FlowLayout
35+
import javax.swing.JCheckBox
36+
import javax.swing.JComponent
37+
import javax.swing.JPanel
38+
39+
class CtorHeadNoUnsafeInspection : MixinInspection() {
40+
@JvmField
41+
var ignoreForFabric = true
42+
43+
override fun createOptionsPanel(): JComponent {
44+
val panel = JPanel(FlowLayout(FlowLayout.LEFT))
45+
val checkbox = JCheckBox("Ignore in Fabric mods", ignoreForFabric)
46+
checkbox.addActionListener {
47+
ignoreForFabric = checkbox.isSelected
48+
}
49+
panel.add(checkbox)
50+
return panel
51+
}
52+
53+
override fun getStaticDescription() = "Reports when @At(\"CTOR_HEAD\") is missing unsafe"
54+
55+
override fun buildVisitor(holder: ProblemsHolder): PsiElementVisitor {
56+
if (ignoreForFabric) {
57+
val isFabric = holder.file.findModule()?.let { MinecraftFacet.getInstance(it) }?.isOfType(FabricModuleType)
58+
?: false
59+
if (isFabric) {
60+
return PsiElementVisitor.EMPTY_VISITOR
61+
}
62+
}
63+
64+
return object : JavaElementVisitor() {
65+
override fun visitAnnotation(annotation: PsiAnnotation) {
66+
if (!annotation.hasQualifiedName(MixinConstants.Annotations.AT)) {
67+
return
68+
}
69+
val valueElement = annotation.findDeclaredAttributeValue("value")
70+
if (valueElement?.constantStringValue != "CTOR_HEAD") {
71+
return
72+
}
73+
if (annotation.findDeclaredAttributeValue("unsafe")?.constantValue == true) {
74+
return
75+
}
76+
holder.registerProblem(
77+
valueElement,
78+
"CTOR_HEAD is missing unsafe = true",
79+
InjectIntoConstructorInspection.AddUnsafeFix(annotation),
80+
)
81+
}
82+
}
83+
}
84+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class InjectIntoConstructorInspection : MixinInspection() {
131131

132132
override fun getStaticDescription() = "@Inject into Constructor"
133133

134-
private class AddUnsafeFix(at: PsiAnnotation) : LocalQuickFixOnPsiElement(at) {
134+
class AddUnsafeFix(at: PsiAnnotation) : LocalQuickFixOnPsiElement(at) {
135135
override fun getFamilyName() = "Add unsafe = true"
136136
override fun getText() = "Add unsafe = true"
137137

src/main/resources/META-INF/plugin.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,14 @@
888888
level="ERROR"
889889
hasStaticDescription="true"
890890
implementationClass="com.demonwav.mcdev.platform.mixin.inspection.injector.CancellableBeforeSuperCallInspection"/>
891+
<localInspection displayName="CTOR_HEAD is missing unsafe = true"
892+
shortName="CtorHeadNoUnsafe"
893+
groupName="Mixin"
894+
language="JAVA"
895+
enabledByDefault="true"
896+
level="ERROR"
897+
hasStaticDescription="true"
898+
implementationClass="com.demonwav.mcdev.platform.mixin.inspection.injector.CtorHeadNoUnsafeInspection"/>
891899
<!--endregion-->
892900

893901
<!--region Overwrite Inspections -->

0 commit comments

Comments
 (0)