Skip to content

Commit

Permalink
Include 'inside' expressions in logical-not-parentheses checking
Browse files Browse the repository at this point in the history
  • Loading branch information
MikePopoloski committed Dec 15, 2024
1 parent 10b0843 commit 7481d7f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
15 changes: 15 additions & 0 deletions source/ast/expressions/OperatorExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<UnaryExpression>().op == UnaryOperator::LogicalNot) {

auto& unary = lhs.as<UnaryExpression>();
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;
}

Expand Down
4 changes: 3 additions & 1 deletion tests/unittests/ast/WarningTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
)");
Expand All @@ -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);
Expand All @@ -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") {
Expand Down

0 comments on commit 7481d7f

Please sign in to comment.