Jump to page: 1 2
Thread overview
Dscanner: intentionally unused variable
Jan 07, 2018
Ivan Trombley
Jan 07, 2018
H. S. Teoh
Jan 07, 2018
Basile B.
Jan 07, 2018
SimonN
Jan 07, 2018
Ivan Trombley
Jan 07, 2018
Basile B.
Jan 07, 2018
Ivan Trombley
Jan 07, 2018
Jacob Carlborg
Jan 07, 2018
visitor
Jan 07, 2018
visitor
Jan 08, 2018
Ivan Trombley
January 07, 2018
While working with SDL, I found that I kept using the same pattern over and over:
- Get the current clip rectangle.
- Set a new clip rectangle.
- restore the old clip rectangle on scope (exit).

Instead of writing that code again and again, I wrote a simple function that returns a struct which restores the old clip rectangle in it's destructor. This works great but now dscanner complains about the variable being unused.

Is there a way tell dscanner that a variable is intentionally unused?
January 06, 2018
On Sun, Jan 07, 2018 at 12:18:27AM +0000, Ivan Trombley via Digitalmars-d wrote:
> While working with SDL, I found that I kept using the same pattern over and
> over:
> - Get the current clip rectangle.
> - Set a new clip rectangle.
> - restore the old clip rectangle on scope (exit).
> 
> Instead of writing that code again and again, I wrote a simple function that returns a struct which restores the old clip rectangle in it's destructor.  This works great but now dscanner complains about the variable being unused.
> 
> Is there a way tell dscanner that a variable is intentionally unused?

IMO, dscanner should be fixed to suppress that warning when the variable in question has a non-trivial dtor that may produce side-effects at the end of the scope. In such cases, it may be that the whole reason for the variable is to trigger the dtor's side-effects, as is your case here, so technically it isn't "unused".


T

-- 
Don't throw out the baby with the bathwater. Use your hands...
January 07, 2018
On Sunday, 7 January 2018 at 00:18:27 UTC, Ivan Trombley wrote:
> While working with SDL, I found that I kept using the same pattern over and over:
> - Get the current clip rectangle.
> - Set a new clip rectangle.
> - restore the old clip rectangle on scope (exit).
>
> Instead of writing that code again and again, I wrote a simple function that returns a struct which restores the old clip rectangle in it's destructor. This works great but now dscanner complains about the variable being unused.
>
> Is there a way tell dscanner that a variable is intentionally unused?

Another way would be to have the RAII wrapper in a with statement, but it produces extra indentation, which you might not like:

    with (MyStruct(100, 200)) {
        // code that uses the new clip rectangle
    }

-- Simon
January 07, 2018
On Sunday, 7 January 2018 at 00:22:15 UTC, H. S. Teoh wrote:
> On Sun, Jan 07, 2018 at 12:18:27AM +0000, Ivan Trombley via Digitalmars-d wrote:
>> While working with SDL, I found that I kept using the same pattern over and
>> over:
>> - Get the current clip rectangle.
>> - Set a new clip rectangle.
>> - restore the old clip rectangle on scope (exit).
>> 
>> Instead of writing that code again and again, I wrote a simple function that returns a struct which restores the old clip rectangle in it's destructor.  This works great but now dscanner complains about the variable being unused.
>> 
>> Is there a way tell dscanner that a variable is intentionally unused?
>
> IMO, dscanner should be fixed to suppress that warning when the variable in question has a non-trivial dtor that may produce side-effects at the end of the scope. In such cases, it may be that the whole reason for the variable is to trigger the dtor's side-effects, as is your case here, so technically it isn't "unused".
>
>
> T

Unfortunately this is not possible without a big refactoring. The struct can be declared in another module and D-Scanner only sees the VariableDeclaration because its scope is limited to the current module.
January 07, 2018
On Sunday, 7 January 2018 at 00:18:27 UTC, Ivan Trombley wrote:
> While working with SDL, I found that I kept using the same pattern over and over:
> - Get the current clip rectangle.
> - Set a new clip rectangle.
> - restore the old clip rectangle on scope (exit).
>
> Instead of writing that code again and again, I wrote a simple function that returns a struct which restores the old clip rectangle in it's destructor. This works great but now dscanner complains about the variable being unused.
>
> Is there a way tell dscanner that a variable is intentionally unused?

You can disable the "unused variable check" for the module in the dscanner.ini file located in the project directory see https://github.com/dlang-community/D-Scanner#selecting-modules-for-a-specific-check.

More simple is to understand D-Scanner limitations and accept that warnings are only warnings and that a message doesn't necessarily mean that there's something to do.
January 07, 2018
On Sunday, 7 January 2018 at 08:46:40 UTC, Basile B. wrote:
> More simple is to understand D-Scanner limitations and accept that warnings are only warnings and that a message doesn't necessarily mean that there's something to do.

If the output is clogged with warnings then it's more difficult to see the actual issues,  limiting the value of dscanner. It makes sense to try to find a reasonable solution.
January 07, 2018
On Sunday, 7 January 2018 at 03:41:18 UTC, SimonN wrote:
> Another way would be to have the RAII wrapper in a with statement, but it produces extra indentation, which you might not like:
>
>     with (MyStruct(100, 200)) {
>         // code that uses the new clip rectangle
>     }
>
> -- Simon

This works good enough. Thanks.
January 07, 2018
On 2018-01-07 01:18, Ivan Trombley wrote:
> While working with SDL, I found that I kept using the same pattern over and over:
> - Get the current clip rectangle.
> - Set a new clip rectangle.
> - restore the old clip rectangle on scope (exit).
> 
> Instead of writing that code again and again, I wrote a simple function that returns a struct which restores the old clip rectangle in it's destructor. This works great but now dscanner complains about the variable being unused.
> 
> Is there a way tell dscanner that a variable is intentionally unused?

I don't know about D-Scanner but I know that other similar tools allow you to prefix the name of a variable with an underscore to indicate that the variable is intentionally unused.

-- 
/Jacob Carlborg
January 07, 2018
On Sunday, 7 January 2018 at 16:14:11 UTC, Jacob Carlborg wrote:
> On 2018-01-07 01:18, Ivan Trombley wrote:

>> Is there a way tell dscanner that a variable is intentionally unused?
>
> I don't know about D-Scanner but I know that other similar tools allow you to prefix the name of a variable with an underscore to indicate that the variable is intentionally unused.

It seems a simple underscore "_" as a variable name tells Dscanner exactly that.
January 07, 2018
On Sunday, 7 January 2018 at 16:29:42 UTC, visitor wrote:
> On Sunday, 7 January 2018 at 16:14:11 UTC, Jacob Carlborg wrote:
>> On 2018-01-07 01:18, Ivan Trombley wrote:
>
>>> Is there a way tell dscanner that a variable is intentionally unused?
>>
>> I don't know about D-Scanner but I know that other similar tools allow you to prefix the name of a variable with an underscore to indicate that the variable is intentionally unused.
>
> It seems a simple underscore "_" as a variable name tells Dscanner exactly that.

Any number of underscores but underscores only apparently.
« First   ‹ Prev
1 2