热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

A'close'annotationtorestrictthebindingsaccessiblebyablock

cchttps://github.com/rust-lang/rfcs/issues/2496(-obk)Itwouldbenicetohave

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




1
x

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
    1
    mut

    , or moves of something not marked

    1
    move

    , but with the understanding that some mutations may slip past it.


  • no warnings about unused
    1
    mut

    or unused

    1
    move

    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.


   



推荐阅读
author-avatar
潇潇小姐姐cherryhv_309
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有