diff --git a/source/ast/expressions/OperatorExpressions.cpp b/source/ast/expressions/OperatorExpressions.cpp index 8b58ed284..596ca86fe 100644 --- a/source/ast/expressions/OperatorExpressions.cpp +++ b/source/ast/expressions/OperatorExpressions.cpp @@ -1569,6 +1569,21 @@ Expression& InsideExpression::fromSyntax(Compilation& compilation, if (bad) return badExpr(compilation, result); + // Warn about `!x inside {y, z}` where they probably meant `!(x inside {y, z})` + auto& lhs = boundSpan[0]->unwrapImplicitConversions(); + if (lhs.kind == ExpressionKind::UnaryOp && !lhs.isParenthesized() && + lhs.as().op == UnaryOperator::LogicalNot) { + + auto& unary = lhs.as(); + auto kindStr = "'inside' expression"sv; + auto& diag = context.addDiag(diag::LogicalNotParentheses, unary.opRange); + diag << kindStr << syntax.inside.range(); + + SourceRange range(unary.operand().sourceRange.start(), result->sourceRange.end()); + diag.addNote(diag::NoteLogicalNotFix, range) << kindStr; + diag.addNote(diag::NoteLogicalNotSilence, lhs.sourceRange); + } + return *result; } diff --git a/tests/unittests/ast/WarningTests.cpp b/tests/unittests/ast/WarningTests.cpp index f4603e75a..5af52416f 100644 --- a/tests/unittests/ast/WarningTests.cpp +++ b/tests/unittests/ast/WarningTests.cpp @@ -1033,6 +1033,7 @@ module m; if ((a + b ? 1 : 2) == 2) begin end if (a < b < c) begin end c |= a & b; + if (!a inside {0, 1, 0}) begin end end endmodule )"); @@ -1041,7 +1042,7 @@ endmodule compilation.addSyntaxTree(tree); auto& diags = compilation.getAllDiagnostics(); - REQUIRE(diags.size() == 10); + REQUIRE(diags.size() == 11); CHECK(diags[0].code == diag::BitwiseRelPrecedence); CHECK(diags[1].code == diag::BitwiseOpParentheses); CHECK(diags[2].code == diag::BitwiseOpParentheses); @@ -1052,6 +1053,7 @@ endmodule CHECK(diags[7].code == diag::BitwiseOpMismatch); CHECK(diags[8].code == diag::ConditionalPrecedence); CHECK(diags[9].code == diag::ConsecutiveComparison); + CHECK(diags[10].code == diag::LogicalNotParentheses); } TEST_CASE("Case statement type warnings") {