MinecraftDev icon indicating copy to clipboard operation
MinecraftDev copied to clipboard

Calls to static @Invoker methods with dummy body cause "unreachable code" warnings

Open TheRealWormbo opened this issue 1 year ago • 6 comments
trafficstars

Minecraft Development for IntelliJ plugin version

2024.1-1.7.4

IntelliJ version

2024.1

Operating System

Linux Mint 21.1

Target platform

Mixins

Description of the bug

Mixins that define an @Invoker for a static target method are required to provide a body for the mixin method, but these methods are usually implemented to always throw an exception, for example:

@Mixin(CriteriaTriggers.class)
public interface CriteriaTriggersAccessor {
	@Invoker("register")
	static <T extends CriterionTrigger<?>> T botania_register(T thing) {
		throw new IllegalStateException();
	}
}

The target method (net.minecraft.advancements.CriteriaTriggers#register) usually completes without throwing an exception. However, code using that invoker method causes following lines to be reported as "unreachable code" by IntelliJ:

public class BotaniaCriteriaTriggers {
	public static void init() {
		CriteriaTriggersAccessor.botania_register(AlfheimPortalTrigger.INSTANCE);
		CriteriaTriggersAccessor.botania_register(CorporeaRequestTrigger.INSTANCE); // unreachable code from here
		CriteriaTriggersAccessor.botania_register(GaiaGuardianNoArmorTrigger.INSTANCE);
		CriteriaTriggersAccessor.botania_register(RelicBindTrigger.INSTANCE);
		CriteriaTriggersAccessor.botania_register(UseItemSuccessTrigger.INSTANCE);
		CriteriaTriggersAccessor.botania_register(ManaBlasterTrigger.INSTANCE);
		CriteriaTriggersAccessor.botania_register(LokiPlaceTrigger.INSTANCE);
		CriteriaTriggersAccessor.botania_register(AlfheimPortalBreadTrigger.INSTANCE);
	}
}

(Code from Botania repository: https://github.com/VazkiiMods/Botania classes vazkii.botania.mixin.CriteriaTriggersAccessor and vazkii.botania.common.advancements.BotaniaCriteriaTriggers from the Xplat module, targeting Minecraft 1.20.1)

TheRealWormbo avatar Apr 20 '24 10:04 TheRealWormbo

2024.1 also creates this problem with regular accessor casts for me.

isXander avatar Apr 28 '24 22:04 isXander

IntelliJ is too smart for its own good.

Earthcomputer avatar Apr 28 '24 23:04 Earthcomputer

I get this issue with casts and instanceof conditions in mixins too (And yes I'm aware good code practice here would be using accessors instead of casting this to Player)

image

image

Thanks intellij :heart_eyes:

valoeghese avatar Apr 30 '24 20:04 valoeghese

Unfortunately this is not easy to fix. IntelliJ's dataflow inspections are hardcoded with no extension points for plugins. There is no way to tell it that actually no, this expression won't throw, and prevent it from highlighting the subsequent code as unused.

So far the inspection has only highlighted the casts themselves, so we were able to check for a mixin cast and suppress the highlight. However this is hard to tell the difference between actually unreachable code and bogus unreachable code without unreliable heuristics.

Earthcomputer avatar Apr 30 '24 20:04 Earthcomputer

Perhaps a pull request adding the required extension points could be made to intellij community edition

valoeghese avatar Apr 30 '24 20:04 valoeghese

Note that JetBrains will be disabling this inspection by default as it is causing problems with other frameworks too: https://youtrack.jetbrains.com/issue/IDEA-352283

Earthcomputer avatar May 08 '24 18:05 Earthcomputer