mirror of
https://github.com/dlang/dmd.git
synced 2025-04-26 21:21:48 +03:00
fix Issue 22134 - Deprecate returning a discarded void value from a function (#12898)
fix Issue 22134 - Deprecate returning a discarded void value from a function Signed-off-by: Martin Kinkelin <kinke@users.noreply.github.com> Signed-off-by: Razvan Nitu <RazvanN7@users.noreply.github.com> Merged-on-behalf-of: unknown
This commit is contained in:
parent
56589f0f4d
commit
81e4682f72
3 changed files with 54 additions and 0 deletions
26
changelog/fix22134.dd
Normal file
26
changelog/fix22134.dd
Normal file
|
@ -0,0 +1,26 @@
|
|||
Returning a discarded void value from a function is now deprecated
|
||||
|
||||
An expression statement of type void that has no side effects should
|
||||
be discarded since it has no effect. The compiler, generally, does not
|
||||
allow such statements, however, in the case of return statements this
|
||||
error is circumvented. For example:
|
||||
|
||||
---
|
||||
struct StackBuffer
|
||||
{
|
||||
auto opIndex(size_t i)
|
||||
{
|
||||
return arr[i];
|
||||
}
|
||||
|
||||
private:
|
||||
void[] arr;
|
||||
}
|
||||
---
|
||||
|
||||
Although this code compiles, any call to `opIndex` is going to result in an
|
||||
error because the return type must either be stored somewhere (and variables
|
||||
cannot be of type void) or the call will have no effect.
|
||||
|
||||
Starting with this release, returning a discarded void value from a function
|
||||
is deprecated. Such code can be deleted as it most certainly is dead code.
|
|
@ -2913,6 +2913,17 @@ package (dmd) extern (C++) final class StatementSemanticVisitor : Visitor
|
|||
rs.exp.type = texp;
|
||||
}
|
||||
|
||||
// @@@DEPRECATED_2.111@@@
|
||||
const olderrors = global.startGagging();
|
||||
// uncomment to turn deprecation into an error when
|
||||
// deprecation cycle is over
|
||||
if (discardValue(rs.exp))
|
||||
{
|
||||
//errors = true;
|
||||
}
|
||||
if (global.endGagging(olderrors))
|
||||
rs.exp.deprecation("`%s` has no effect", rs.exp.toChars());
|
||||
|
||||
/* Replace:
|
||||
* return exp;
|
||||
* with:
|
||||
|
|
17
test/fail_compilation/fail22134.d
Normal file
17
test/fail_compilation/fail22134.d
Normal file
|
@ -0,0 +1,17 @@
|
|||
// https://issues.dlang.org/show_bug.cgi?id=22134
|
||||
/* REQUIRED_ARGS: -de
|
||||
TEST_OUTPUT:
|
||||
---
|
||||
fail_compilation/fail22134.d(12): Deprecation: `this.arr[i]` has no effect
|
||||
---
|
||||
*/
|
||||
struct StackBuffer
|
||||
{
|
||||
auto opIndex(size_t i)
|
||||
{
|
||||
return arr[i];
|
||||
}
|
||||
|
||||
private:
|
||||
void[] arr;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue