Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #20583 - Initialisation of @mustuse member variable causes spur… #20584

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion compiler/src/dmd/mustuse.d
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
{
auto sd = sym.isStructDeclaration();
// isStructDeclaration returns non-null for both structs and unions
if (sd && hasMustUseAttribute(sd, sc) && !isAssignment(e) && !isIncrementOrDecrement(e))
if (sd && hasMustUseAttribute(sd, sc)
&& !isAssignment(e)
&& !isCtorAssignment(e)
&& !isIncrementOrDecrement(e))
{
error(e.loc, "ignored value of `@%s` type `%s`; prepend a `cast(void)` if intentional",
Id.udaMustUse.toChars(), e.type.toPrettyChars(true));
Expand Down Expand Up @@ -104,6 +107,25 @@
return false;
}

/**
* Returns: true if `e` is an assignment that's been rewritten to a
* constructor call.
*/
private bool isCtorAssignment(Expression e)
{
import dmd.id : Id;

auto comma = e.isCommaExp();
if (!comma)
return false;
if (!isAssignment(comma.e1))
return false;
auto call = comma.e2.isCallExp();
if (!call)
return false;

Check warning on line 125 in compiler/src/dmd/mustuse.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/mustuse.d#L125

Added line #L125 was not covered by tests
return call.f.ident == Id.ctor;
}

/**
* Returns: true if id is the identifier of an assignment operator overload.
*/
Expand Down
14 changes: 14 additions & 0 deletions compiler/test/compilable/must_use_assign.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,17 @@ void test()
S a, b;
a = b;
}

@mustuse struct Inner
{
this(int n) {}
}

struct Outer
{
Inner inner;
this(int n)
{
this.inner = n;
}
}
Loading