Skip to content
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

Lint for redundant positional arguments. #1770

Merged
Merged
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
8 changes: 8 additions & 0 deletions Content.Tests/DMProject/Tests/Call/RedundantPositionalKey.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// COMPILE ERROR

#pragma PointlessPositionalArgument error

/proc/test()

/proc/RunTest()
test(1 = "a", 2 = "b", 3 = "c") // 3 unnecessary positional keys
1 change: 1 addition & 0 deletions DMCompiler/Compiler/CompilerError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
SuspiciousMatrixCall = 2207, // Calling matrix() with seemingly the wrong arguments
FallbackBuiltinArgument = 2208, // A builtin (sin(), cos(), etc) with an invalid/fallback argument
PointlessScopeOperator = 2209,
PointlessPositionalArgument = 2210,
MalformedRange = 2300,
InvalidRange = 2301,
InvalidSetStatement = 2302,
Expand Down Expand Up @@ -138,7 +139,7 @@
/// This should be ideally used for exceptions that are the fault of the compiler itself, <br/>
/// like an abnormal state being reached or something.
/// </summary>
public sealed class CompileAbortException : CompileErrorException {

Check warning on line 142 in DMCompiler/Compiler/CompilerError.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

'CompileErrorException' is obsolete: 'This is not a desirable way for the compiler to emit an error. Use CompileAbortException or ForceError() if it needs to be fatal, or an DMCompiler.Emit() otherwise.'
public CompileAbortException(CompilerEmission error) : base(error) {
}

Expand All @@ -149,6 +150,6 @@
}
}

public sealed class UnknownIdentifierException(Location location, string identifierName) : CompileErrorException(location, $"Unknown identifier \"{identifierName}\"") {

Check warning on line 153 in DMCompiler/Compiler/CompilerError.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

'CompileErrorException' is obsolete: 'This is not a desirable way for the compiler to emit an error. Use CompileAbortException or ForceError() if it needs to be fatal, or an DMCompiler.Emit() otherwise.'
public string IdentifierName = identifierName;
}
9 changes: 8 additions & 1 deletion DMCompiler/DM/DMExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,14 @@ public ArgumentList(Location location, DMObject dmObject, DMProc proc, DMASTCall
break;
case Expressions.Number keyNum:
//Replaces an ordered argument
argIndex = (int)keyNum.Value;
var newIdx = (int)keyNum.Value - 1;

if (newIdx == argIndex) {
DMCompiler.Emit(WarningCode.PointlessPositionalArgument, key.Location,
$"The argument at index {argIndex + 1} is a positional argument with a redundant index (\"{argIndex + 1} = value\" at argument {argIndex + 1}). This does not function like a named argument and is likely a mistake.");
}

argIndex = newIdx;
break;
case Expressions.Resource _:
case Expressions.ConstantPath _:
Expand Down
1 change: 1 addition & 0 deletions DMCompiler/DMStandard/DefaultPragmaConfig.dm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#pragma MissingInterpolatedExpression warning
#pragma AmbiguousResourcePath warning
#pragma SuspiciousSwitchCase warning
#pragma PointlessPositionalArgument warning
// NOTE: The next few pragmas are for OpenDream's experimental type checker
// This feature is still in development, elevating these pragmas outside of local testing is discouraged
// An RFC to finalize this feature is coming soon(TM)
Expand Down
Loading