Skip to content

Commit

Permalink
Add Wuseless-cast
Browse files Browse the repository at this point in the history
  • Loading branch information
MikePopoloski committed Dec 27, 2023
1 parent b71c534 commit 4177d5b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
4 changes: 3 additions & 1 deletion scripts/diagnostics.txt
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ warning negative-timing-limit NegativeTimingLimit "timing check limit value '{}'
warning sign-conversion SignConversion "implicit conversion changes signedness from {} to {}"
warning float-bool-conv FloatBoolConv "implicit conversion from floating point type {} to boolean value"
warning int-bool-conv IntBoolConv "implicit conversion from {} to boolean value"
warning useless-cast UselessCast "useless cast from {} to the same type"

subsystem Statements
error ReturnNotInSubroutine "return statement is only valid inside task and function blocks"
Expand Down Expand Up @@ -1054,7 +1055,8 @@ group default = { real-underflow real-overflow vector-overflow int-overflow unco

group extra = { empty-member empty-stmt dup-import pointless-void-cast case-gen-none case-gen-dup
unused-result format-real ignored-slice task-ignored width-trunc dup-attr event-const
ineffective-sign port-width-trunc constant-conversion float-bool-conv int-bool-conv }
ineffective-sign port-width-trunc constant-conversion float-bool-conv int-bool-conv
useless-cast }

group pedantic = { empty-pattern implicit-net-port nonstandard-escape-code nonstandard-generate format-multibit-strength
nonstandard-sys-func nonstandard-foreach nonstandard-dist }
Expand Down
9 changes: 9 additions & 0 deletions scripts/warning_docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1117,3 +1117,12 @@ module m;
initial if (i + 2) begin end // Did you mean i + 2 != 0 or something else?
endmodule
```

-Wuseless-cast
An explicit cast converts an expression to the same type, so the cast does nothing.
```
module m;
int i, j;
assign i = int'(j);
endmodule
```
6 changes: 6 additions & 0 deletions source/ast/expressions/AssignmentExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ Expression& ConversionExpression::fromSyntax(Compilation& compilation,
if (!Bitstream::checkClassAccess(*type, context, targetExpr.sourceRange)) {
return badExpr(compilation, result());
}

if (operand.kind == ExpressionKind::Streaming) {
if (!Bitstream::isBitstreamCast(*type,
operand.as<StreamingConcatenationExpression>())) {
Expand All @@ -827,6 +828,11 @@ Expression& ConversionExpression::fromSyntax(Compilation& compilation,
return *result(ConversionKind::BitstreamCast);
}

if (type->isMatching(*operand.type)) {
context.addDiag(diag::UselessCast, syntax.apostrophe.location())
<< *operand.type << targetExpr.sourceRange << operand.sourceRange;
}

return *result();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/ast/EvalTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ TEST_CASE("Dynamic string ops") {
CHECK(session.eval("{\"Hi\",str2}").str() == "Hiaaaaa");
CHECK(session.eval("str2 = {\"Hi\", \"Bye\"}").str() == "HiBye");

CHECK(session.eval("string'(str1)").str() == "a");
CHECK(session.eval("str1").str() == "a");

session.eval("byte ba[] = \"asdf\";");
CHECK(session.eval("string'(ba)").str() == "asdf");
Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/ast/MemberTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2065,7 +2065,7 @@ module m(input [4:0] a, output [4:0] b, z[6], output [5:0] l, I.m foo, I bar);
if (k < 2) (a => z[1]) = 1;
if (1 < 2) (a => z[2]) = 1;
if (int'(g) == 1) (a => z[3]) = 1;
if (byte'(g) == 1) (a => z[3]) = 1;
if (+g == 1) (a => z[4]) = 1;
if (g inside { 1, 2 }) (a => z[5]) = 1;
Expand Down
20 changes: 20 additions & 0 deletions tests/unittests/ast/WarningTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,3 +769,23 @@ endmodule
CHECK(diags[0].code == diag::FloatBoolConv);
CHECK(diags[1].code == diag::IntBoolConv);
}

TEST_CASE("Useless cast warnings") {
auto tree = SyntaxTree::fromText(R"(
module m;
int i, j;
assign i = int'(j);
logic [3:0] k, l;
assign k = 4'(l);
endmodule
)");

Compilation compilation;
compilation.addSyntaxTree(tree);

auto& diags = compilation.getAllDiagnostics();
REQUIRE(diags.size() == 2);
CHECK(diags[0].code == diag::UselessCast);
CHECK(diags[1].code == diag::UselessCast);
}

0 comments on commit 4177d5b

Please sign in to comment.