summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYong He <yonghe@outlook.com>2023-04-05 13:37:22 -0700
committerGitHub <noreply@github.com>2023-04-05 13:37:22 -0700
commitc9eb594cefa0659639aae641dc6847c92196dc89 (patch)
tree9495dce0aa30f83a4e7eeb4605871f8e40e5f26f
parentdc45802707b6e3f1a3eedc8e8a5583102b2d8a0d (diff)
Warn on dangling comparison operator. (#2779)
Fixes #1685 Co-authored-by: Yong He <yhe@nvidia.com>
-rw-r--r--source/slang/slang-check-stmt.cpp10
-rw-r--r--source/slang/slang-diagnostic-defs.h1
-rw-r--r--tests/diagnostics/dangling-comparison.slang10
-rw-r--r--tests/diagnostics/dangling-comparison.slang.expected8
4 files changed, 29 insertions, 0 deletions
diff --git a/source/slang/slang-check-stmt.cpp b/source/slang/slang-check-stmt.cpp
index a61f48af6..6efc3e52e 100644
--- a/source/slang/slang-check-stmt.cpp
+++ b/source/slang/slang-check-stmt.cpp
@@ -339,6 +339,16 @@ namespace Slang
void SemanticsStmtVisitor::visitExpressionStmt(ExpressionStmt *stmt)
{
stmt->expression = CheckExpr(stmt->expression);
+ if (auto operatorExpr = as<OperatorExpr>(stmt->expression))
+ {
+ if (auto func = as<VarExpr>(operatorExpr->functionExpr))
+ {
+ if (func->name && func->name->text == "==")
+ {
+ getSink()->diagnose(operatorExpr, Diagnostics::danglingEqualityExpr);
+ }
+ }
+ }
}
void SemanticsStmtVisitor::tryInferLoopMaxIterations(ForStmt* stmt)
diff --git a/source/slang/slang-diagnostic-defs.h b/source/slang/slang-diagnostic-defs.h
index fed4944d3..4a9b83c6c 100644
--- a/source/slang/slang-diagnostic-defs.h
+++ b/source/slang/slang-diagnostic-defs.h
@@ -278,6 +278,7 @@ DIAGNOSTIC(30054, Error, targetLabelDoesNotMarkBreakableStmt, "invalid break tar
DIAGNOSTIC(30055, Error, useOfNonShortCircuitingOperatorInDiffFunc, "non-short-circuiting `?:` operator is not allowed in a differentiable function, use `select` instead.")
DIAGNOSTIC(30056, Warning, useOfNonShortCircuitingOperator, "non-short-circuiting `?:` operator is deprecated, use 'select' instead.")
DIAGNOSTIC(30057, Error, assignmentInPredicateExpr, "use an assignment operation as predicate expression is not allowed, wrap the assignment with '()' to clarify the intent.")
+DIAGNOSTIC(30058, Warning, danglingEqualityExpr, "result of '==' not used, did you intend '='?")
DIAGNOSTIC(30043, Error, getStringHashRequiresStringLiteral, "getStringHash parameter can only accept a string literal")
diff --git a/tests/diagnostics/dangling-comparison.slang b/tests/diagnostics/dangling-comparison.slang
new file mode 100644
index 000000000..8f0f4b2a5
--- /dev/null
+++ b/tests/diagnostics/dangling-comparison.slang
@@ -0,0 +1,10 @@
+//DIAGNOSTIC_TEST:SIMPLE:
+
+[numthreads(1, 1, 1)]
+void main(
+ uint3 dispatchThreadID : SV_DispatchThreadID)
+{
+ int a = 1;
+ a == 2; // warn
+ (a == 2); // ok.
+} \ No newline at end of file
diff --git a/tests/diagnostics/dangling-comparison.slang.expected b/tests/diagnostics/dangling-comparison.slang.expected
new file mode 100644
index 000000000..667e2c17a
--- /dev/null
+++ b/tests/diagnostics/dangling-comparison.slang.expected
@@ -0,0 +1,8 @@
+result code = 0
+standard error = {
+tests/diagnostics/dangling-comparison.slang(8): warning 30058: result of '==' not used, did you intend '='?
+ a == 2; // warn
+ ^~
+}
+standard output = {
+}