作者:潇潇小姐姐cherryhv_309 | 来源:互联网 | 2023-08-30 11:44
cc https://github.com/rust-lang/rfcs/issues/2496 ( -obk )
It would be nice to have a way to just restrict the bindings accessible by a block of code, kinda like one of the early planned features of JAI. -obk suggested this could be done as a clippy lint, which sounds neat.
I'm opening this issue so we can bikeshed the syntax, discuss limitations, and possibly implement it (or to give people a chance to object to it being implemented in clippy).
Scattered thoughts/discussion starters in the next comment.
该提问来源于开源项目:rust-lang/rust-clippy
I tried to look into this and quickly ran into a possible implementation challenge, for which I created #2916. Long story short, it does not appear to me that there is currently any reliable way for clippy to identify that all of these
s refer to the same variable:
1 2 3 4 5 6 7 8
| rust
let x = vec![1, 2];
if true {
println!("{:?}", x.last());
(|| {
x.sort();
})();
} |
which I guess really isn't a problem for most lints as they can just give up in cases of uncertainty, but for a feature like this, I would really want the compiler's warnings to be precise. (otherwise, how can I trust the annotations I have written?)
Maybe in the interim, a reduction in the scope of the lint is necessary; i.e. to pick some subset of the warnings that still provide some meaningful information even if there are some false negatives.
-obk's original suggestion might be easier to do (perhaps he foresaw these technical difficulties! :wink:):
- allow any variable to be read
- make an effort to warn on mutations of something not marked
, or moves of something not marked
, but with the understanding that some mutations may slip past it.
- no warnings about unused
or unused
annotations for now.
I think this should be a lot easier to implement, though I'm not sure if I would find it very useful.