www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - DMD compiler - warning of unused variables

reply DLearner <bmqazwsx123 gmail.com> writes:
Hi

Please see code below:
```
void main() {

    import std.stdio;

    size_t i;
    size_t j;

    i = 5;

    writeln("i = ",i);

}

```

Is there a compiler option that would warn that variable 'j' is 
defined but not used?

Best regards
Aug 16 2021
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
No.

https://github.com/dlang-community/D-Scanner#implemented-checks
Aug 16 2021
prev sibling next sibling parent jfondren <julian.fondren gmail.com> writes:
On Monday, 16 August 2021 at 14:14:27 UTC, DLearner wrote:
 Hi

 Please see code below:
 ```
 void main() {

    import std.stdio;

    size_t i;
    size_t j;

    i = 5;

    writeln("i = ",i);

 }

 ```

 Is there a compiler option that would warn that variable 'j' is 
 defined but not used?

 Best regards
dmd is anti-warning, so it'd have to be an error, which might be more annoying than it's worth. `dscanner --report thefile.d` includes "Variable j is never modified and could have been declared const or immutable." and "Variable j is never used." as issues, with line and column numbers.
Aug 16 2021
prev sibling next sibling parent user1234 <user1234 12.de> writes:
On Monday, 16 August 2021 at 14:14:27 UTC, DLearner wrote:
 Is there a compiler option that would warn that variable 'j' is 
 defined but not used?

 Best regards
No in DMD but you can use [D-Scanner](https://code.dlang.org/packages/dscanner) for that. The check works reasonably as long as the variables are not introduced by `mixin()` or metaprog.
Aug 16 2021
prev sibling parent russhy <russhy gmail.com> writes:
On Monday, 16 August 2021 at 14:14:27 UTC, DLearner wrote:
 Hi

 Please see code below:
 ```
 void main() {

    import std.stdio;

    size_t i;
    size_t j;

    i = 5;

    writeln("i = ",i);

 }

 ```

 Is there a compiler option that would warn that variable 'j' is 
 defined but not used?

 Best regards
Hi, not compiler, but with dscanner it is possible, in fact with the code-d vscode extension, you get that out of the box with visual hints! Take a look at this screenshot: https://i.imgur.com/TGkgY3e.png If you click on the line, it jumps automatically to the right line, very nice tooling
Aug 16 2021