Skip to content

Add warning for ambiguous arg subtraction #7808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev/patch
Choose a base branch
from
Open
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
32 changes: 21 additions & 11 deletions src/main/java/ch/njol/skript/expressions/ExprArgument.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
package ch.njol.skript.expressions;

import java.util.List;
import java.util.regex.MatchResult;

import ch.njol.skript.lang.EventRestrictedSyntax;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.ServerCommandEvent;
import org.jetbrains.annotations.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.command.Argument;
Expand All @@ -19,6 +9,7 @@
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.lang.EventRestrictedSyntax;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.Literal;
Expand All @@ -29,6 +20,14 @@
import ch.njol.skript.util.Utils;
import ch.njol.util.Kleenean;
import ch.njol.util.StringUtils;
import ch.njol.util.coll.CollectionUtils;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.ServerCommandEvent;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.regex.MatchResult;

@Name("Argument")
@Description({
Expand Down Expand Up @@ -65,7 +64,9 @@ public class ExprArgument extends SimpleExpression<Object> implements EventRestr
private Argument<?> argument;

private int ordinal = -1; // Available in ORDINAL and sometimes CLASSINFO


private boolean couldCauseArithmeticConfusion = false;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
Expand All @@ -81,6 +82,8 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
break;
case 3:
what = parseResult.mark == 1 ? ALL : SINGLE;
if (what == SINGLE && parseResult.expr.matches("(the )?arg(ument)?"))
couldCauseArithmeticConfusion = true; // 'arg-1' could be parsed as 'argument - 1'
break;
case 4:
case 5:
Expand Down Expand Up @@ -246,6 +249,13 @@ protected Object[] get(final Event e) {
public boolean isSingle() {
return argument != null ? argument.isSingle() : what != ALL;
}

/**
* @return whether the expression is 'arg', a single argument that could cause confusion with 'arg-1' being parsed as 'argument - 1'.
*/
public boolean couldCauseArithmeticConfusion() {
return couldCauseArithmeticConfusion;
}

@Override
public Class<?> getReturnType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
import ch.njol.skript.doc.Since;
import ch.njol.skript.expressions.ExprArgument;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.Literal;
Expand All @@ -26,8 +27,8 @@

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;
import java.util.List;

@Name("Arithmetic")
@Description("Arithmetic expressions, e.g. 1 + 2, (health of player - 2) / 3, etc.")
Expand Down Expand Up @@ -113,6 +114,9 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
rightGrouped = patternInfo.rightGrouped;
operator = patternInfo.operator;

// print warning for arg-1 confusion scenario
printArgWarning(first, second, operator);

/*
* Step 1: UnparsedLiteral Resolving
*
Expand Down Expand Up @@ -294,6 +298,22 @@ public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelaye
return arithmeticGettable != null || error(firstClass, secondClass);
}

private void printArgWarning(Expression<L> first, Expression<R> second, Operator operator) {
if (operator == Operator.SUBTRACTION && !rightGrouped && !leftGrouped // if the operator is '-' and the user didn't use ()
&& first instanceof ExprArgument argument && argument.couldCauseArithmeticConfusion() // if the first expression is 'arg'
&& second instanceof ExprArithmetic<?, ?, ?> secondArith && secondArith.first instanceof Literal<?> literal // this ambiguity only occurs when the code is parsed as `arg - (1 * 2)` or a similar PEMDAS priority.
&& literal.canReturn(Number.class)) {
// ensure that the second literal is a 1
Literal<?> secondLiteral = (Literal<?>) LiteralUtils.defendExpression(literal);
if (LiteralUtils.canInitSafely(secondLiteral)) {
double number = ((Number) secondLiteral.getSingle()).doubleValue();
if (number == 1)
Skript.warning("This subtraction is ambiguous and could be interpreted either as 'arg-1' or as '(arg) - 1'. " +
"If you meant to use 'arg-1', use 'arg 1' instead or use parentheses to clarify your intent.");
}
}
}

@Override
@SuppressWarnings("unchecked")
protected T[] get(Event event) {
Expand Down