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

linter for useless ref.read #3906

Open
stephane-archer opened this issue Jan 6, 2025 · 8 comments
Open

linter for useless ref.read #3906

stephane-archer opened this issue Jan 6, 2025 · 8 comments
Assignees
Labels
enhancement New feature or request linter

Comments

@stephane-archer
Copy link
Contributor

I found in my code a ref.read without a returned value capture:

ref.read(myProvider)

it would be great if Riverpod linter would help detect stupid mistakes like these

@rrousselGit
Copy link
Owner

Technically that ref.read may not be useless. Some folks use ref.read to eagerly initialize their providers.

I'm not sure whether we'd truly want to ban ""useless"" reads as it's unclear whether they truly are useless or not.
But if we do agree, then adding a @useResult on the method should be enough :)

@stephane-archer
Copy link
Contributor Author

@rrousselGit can we bypass "@useResult"? That way we can explicitly say that we want that read (probably for initialization purposes)

@rrousselGit
Copy link
Owner

There's no official way to bypass it, no (besides // ignore: ... but that doesn't count)

@glemartret
Copy link

I just looked, adding the @useResult seems indeed a bit strict, it could be useful in the case of a read on a autodisposed provider

@stephane-archer
Copy link
Contributor Author

"ref.readForInit(myProvider)"
That doesn't return anything
and "ref.read(myProvider)" with "@UseResult"

Would this make sense and make things explicit?

If we have family and auto dispose on the provider, can't we have notLazy to avoid valid use of "ref.read(myProvider)" in the first place?

@rrousselGit
Copy link
Owner

"ref.readForInit(myProvider)"

I'd rather not add a new API just for that. I'm trying to reduce the API surface, not increase it for very little value.

If we have family and auto dispose on the provider, can't we have notLazy to avoid valid use of "ref.read(myProvider)" in the first place?

Not doable

@stephane-archer
Copy link
Contributor Author

Do you see any better option than // ignore: useResult I want my provider not lazy ?

@rrousselGit
Copy link
Owner

Not immediately, and I don't think that's worth the cost either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request linter
Projects
None yet
Development

No branches or pull requests

3 participants