Skip to content

Commit

Permalink
Adds a warning when assigning a value in a conditional (#1596)
Browse files Browse the repository at this point in the history
* adds a warning when assigning a value in a conditional

* review request
  • Loading branch information
Zonespace27 authored Jan 1, 2024
1 parent c1d4fab commit fa34287
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
7 changes: 6 additions & 1 deletion DMCompiler/Compiler/DM/DMParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,12 @@ private DMASTProcStatementSet[] ProcSetEnd(bool allowMultiple) {
Consume(TokenType.DM_LeftParenthesis, "Expected '('");
BracketWhitespace();
DMASTExpression? condition = Expression();
if (condition == null) Error("Expected a condition");
if (condition == null) {
Error("Expected a condition");
}
if (condition is DMASTAssign) {
DMCompiler.Emit(WarningCode.AssignmentInConditional, condition.Location, "Assignment in conditional");
}
BracketWhitespace();
ConsumeRightParenthesis();
Whitespace();
Expand Down
2 changes: 1 addition & 1 deletion DMCompiler/DMStandard/DefaultPragmaConfig.dm
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@
#pragma EmptyBlock notice
#pragma EmptyProc disabled // NOTE: If you enable this in OD's default pragma config file, it will emit for OD's DMStandard. Put it in your codebase's pragma config file.
#pragma UnsafeClientAccess disabled // NOTE: Only checks for unsafe accesses like "client.foobar" and doesn't consider if the client was already null-checked earlier in the proc

#pragma AssignmentInConditional warning
1 change: 1 addition & 0 deletions OpenDreamShared/Compiler/CompilerError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public enum WarningCode {
EmptyProc = 3101,
UnsafeClientAccess = 3200,
SuspiciousSwitchCase = 3201, // "else if" cases are actually valid DM, they just spontaneously end the switch context and begin an if-else ladder within the else case of the switch
AssignmentInConditional = 3202,

// 4000 - 4999 are reserved for runtime configuration. (TODO: Runtime doesn't know about configs yet!)
}
Expand Down

0 comments on commit fa34287

Please sign in to comment.