Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't emit "XDeclaredButNotUsed" hint for instances of types with custom destructor #24728

Open
arnetheduck opened this issue Feb 26, 2025 · 1 comment

Comments

@arnetheduck
Copy link
Contributor

Description

Destructors are useful for RAII-style scope-based utilities. As such, they can be seen as a better and more flexible defer, given that they are reusable.

However, to use them as such, one needs to create an instance somewhere whose only purpose is to go out of scope at the right moment and that variable will never be used by regular code - the custom destructor itself is the usage.

type Defer = object

proc `=destroy`(x: X) = echo "this is the meaningful part of using Defer"

proc f() =
  var x: Defer # this should result in no hint

As an alternative, a separate hint could be given for the code above - this would allow applications to turn the normal "XDeclaredButNotUsed" hint into an error while leaving this usecase alone.

Nim Version

2.2.0

Current Output


Expected Output


Known Workarounds

No response

Additional Information

No response

@ringabout ringabout assigned ringabout and unassigned ringabout Feb 26, 2025
@Araq
Copy link
Member

Araq commented Feb 26, 2025

Shouldn't this code be:

proc f() =
  var _: Defer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants