Debian Codegraph @

gcc/d/dmd/mustuse.d

raw
1 /**
2 * Compile-time checks associated with the @mustuse attribute.
3 *
4 * Copyright: Copyright (C) 2022-2025 by The D Language Foundation, All Rights Reserved
5 * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
6 * Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/mustuse.d, _mustuse.d)
7 * Documentation: https://dlang.org/phobos/dmd_mustuse.html
8 * Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/mustuse.d
9 */
10
11 module dmd.mustuse;
12
13 import dmd.dscope;
14 import dmd.dsymbol;
15 import dmd.errors;
16 import dmd.expression;
17 import dmd.globals;
18 import dmd.identifier;
19 import dmd.location;
20
21 /**
22 * Check whether discarding an expression would violate the requirements of
23 * @mustuse. If so, emit an error.
24 *
25 * Params:
26 * e = the expression to check
27 * sc = scope in which `e` was semantically analyzed
28 *
29 * Returns: true on error, false on success.
30 */
31 bool checkMustUse(Expression e, Scope* sc)
32 {
33 import dmd.id : Id;
34 import dmd.typesem : toDsymbol;
35
36 assert(e.type);
37 if (auto sym = e.type.toDsymbol(sc))
38 {
39 auto sd = sym.isStructDeclaration();
40 // isStructDeclaration returns non-null for both structs and unions
41 if (sd && hasMustUseAttribute(sd, sc) && !isAssignment(e) && !isIncrementOrDecrement(e))
42 {
43 error(e.loc, "ignored value of `@%s` type `%s`; prepend a `cast(void)` if intentional",
44 Id.udaMustUse.toChars(), e.type.toPrettyChars(true));
45 return true;
46 }
47 }
48 return false;
49 }
50
51 /**
52 * Called from a symbol's semantic to check for reserved usage of @mustuse.
53 *
54 * If such usage is found, emits an errror.
55 *
56 * Params:
57 * sym = symbol to check
58 */
59 void checkMustUseReserved(Dsymbol sym)
60 {
61 import dmd.attrib : foreachUdaNoSemantic;
62 import dmd.errors : error;
63 import dmd.id : Id;
64
65 // Can't use foreachUda (and by extension hasMustUseAttribute) while
66 // semantic analysis of `sym` is still in progress
67 foreachUdaNoSemantic(sym, (exp) {
68 if (isMustUseAttribute(exp))
69 {
70 if (sym.isFuncDeclaration())
71 {
72 error(sym.loc, "`@%s` on functions is reserved for future use",
73 Id.udaMustUse.toChars());
74 sym.errors = true;
75 }
76 else if (sym.isClassDeclaration() || sym.isEnumDeclaration())
77 {
78 error(sym.loc, "`@%s` on `%s` types is reserved for future use",
79 Id.udaMustUse.toChars(), sym.kind());
80 sym.errors = true;
81 }
82 }
83 return 0; // continue
84 });
85 }
86
87 /**
88 * Returns: true if the given expression is an assignment, either simple (a = b)
89 * or compound (a += b, etc).
90 */
91 private bool isAssignment(Expression e)
92 {
93 if (e.isAssignExp || e.isBinAssignExp || e.isConstructExp || e.isBlitExp)
94 return true;
95 if (auto ce = e.isCallExp())
96 {
97 if (auto fd = ce.f)
98 {
99 auto id = fd.ident;
100 if (id && isAssignmentOpId(id))
101 return true;
102 }
103 }
104 return false;
105 }
106
107 /**
108 * Returns: true if id is the identifier of an assignment operator overload.
109 */
110 private bool isAssignmentOpId(Identifier id)
111 {
112 import dmd.id : Id;
113
114 return id == Id.opAssign
115 || id == Id.opIndexAssign
116 || id == Id.opSlice
117 || id == Id.opSliceAssign
118 || id == Id.opOpAssign
119 || id == Id.opIndexOpAssign
120 || id == Id.opSliceOpAssign;
121 }
122
123 /**
124 * Returns: true if the given expression is an increment (++) or decrement (--).
125 */
126 private bool isIncrementOrDecrement(Expression e)
127 {
128 import dmd.dtemplate : isExpression;
129 import dmd.location;
130 import dmd.id : Id;
131 import dmd.tokens : EXP;
132
133 if (e.op == EXP.plusPlus
134 || e.op == EXP.minusMinus
135 || e.op == EXP.prePlusPlus
136 || e.op == EXP.preMinusMinus)
137 return true;
138 if (auto call = e.isCallExp())
139 {
140 // Check for overloaded preincrement
141 // e.g., a.opUnary!"++"
142 if (auto fd = call.f)
143 {
144 if (fd.ident == Id.opUnary && fd.parent)
145 {
146 if (auto ti = fd.parent.isTemplateInstance())
147 {
148 auto tiargs = ti.tiargs;
149 if (tiargs && tiargs.length >= 1)
150 {
151 if (auto argExp = (*tiargs)[0].isExpression())
152 {
153 if (auto op = argExp.isStringExp())
154 {
155 if (op.len == 2 && op.sz == 1)
156 {
157 const s = op.peekString();
158 if (s == "++" || s == "--")
159 return true;
160 }
161 }
162 }
163 }
164 }
165 }
166 }
167 }
168 else if (auto comma = e.isCommaExp())
169 {
170 // Check for overloaded postincrement
171 // e.g., (auto tmp = a, ++a, tmp)
172 if (comma.e1)
173 {
174 if (auto left = comma.e1.isCommaExp())
175 {
176 if (auto middle = left.e2)
177 {
178 if (middle && isIncrementOrDecrement(middle))
179 return true;
180 }
181 }
182 }
183 }
184 return false;
185 }
186
187 /**
188 * Returns: true if the given symbol has the @mustuse attribute.
189 */
190 private bool hasMustUseAttribute(Dsymbol sym, Scope* sc)
191 {
192 import dmd.attribsem : foreachUda;
193
194 bool result = false;
195
196 foreachUda(sym, sc, (Expression uda) {
197 if (isMustUseAttribute(uda))
198 {
199 result = true;
200 return 1; // break
201 }
202 return 0; // continue
203 });
204
205 return result;
206 }
207
208 /**
209 * Returns: true if the given expression is core.attribute.mustuse.
210 */
211 private bool isMustUseAttribute(Expression e)
212 {
213 import dmd.attrib : isEnumAttribute;
214 import dmd.id : Id;
215 return isEnumAttribute(e, Id.udaMustUse);
216 }