Auto function without return statement, prefer an explicit void
user1234
user1234 at 12.de
Thu May 19 10:48:27 UTC 2022
On Thursday, 19 May 2022 at 09:24:21 UTC, user1234 wrote:
> On Thursday, 19 May 2022 at 09:15:50 UTC, Anonymouse wrote:
>> I like explicit return types. But I also like inference.
>>
>> ```d
>> struct SomeContainer(T)
>> {
>> /* ... */
>> auto clear()
>> {
>> head = 0;
>> tail = 0;
>> buf[] = T.init;
>> }
>> }
>> ```
>>
>> dscanner -S: `Auto function without return statement, prefer
>> an explicit void`
>>
>> Can we please have `void clear() auto`?
>
> The problem is when in the body you have an `asm` block that
> returns.
> In that case the return type cant be inferred.
>
> ```d
> module runnable ;
>
> auto notVoid()
> {
> asm
> {
> naked;
> mov AL, 1;
> ret;
> }
> }
>
> void main()
> {
> auto b = notVoid();
> }
> ```
>
>> Error: variable `runnable.main.b` type `void` is inferred from
>> initializer `notVoid()`, and variables cannot be of type `void`
>
> but `notVoid()` actually returns a `ubyte`
so... we got an error here because we use the function. The
D-Scanner check is just here to show you that "hello there might
be a small problem here", and that's it ;). Linting is very
opritinatrist.
The logic is
- return type is auto
- there's not return statments
=> take care
More information about the Digitalmars-d
mailing list