Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
88f4063c
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
88f4063c
编写于
5月 17, 2018
作者:
K
Kyle Stachowicz
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Reimplement unused_labels lint as a compiler builtin in the resolver
上级
7676982e
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
170 addition
and
21 deletion
+170
-21
src/librustc/lint/builtin.rs
src/librustc/lint/builtin.rs
+7
-0
src/librustc_resolve/check_unused.rs
src/librustc_resolve/check_unused.rs
+4
-0
src/librustc_resolve/lib.rs
src/librustc_resolve/lib.rs
+10
-2
src/test/ui/lint/unused_labels.rs
src/test/ui/lint/unused_labels.rs
+75
-0
src/test/ui/lint/unused_labels.stderr
src/test/ui/lint/unused_labels.stderr
+53
-0
src/test/ui/loops-reject-duplicate-labels-2.rs
src/test/ui/loops-reject-duplicate-labels-2.rs
+1
-0
src/test/ui/loops-reject-duplicate-labels-2.stderr
src/test/ui/loops-reject-duplicate-labels-2.stderr
+9
-9
src/test/ui/loops-reject-duplicate-labels.rs
src/test/ui/loops-reject-duplicate-labels.rs
+1
-0
src/test/ui/loops-reject-duplicate-labels.stderr
src/test/ui/loops-reject-duplicate-labels.stderr
+9
-9
src/test/ui/suggestions/suggest-labels.rs
src/test/ui/suggestions/suggest-labels.rs
+1
-1
未找到文件。
src/librustc/lint/builtin.rs
浏览文件 @
88f4063c
...
...
@@ -279,6 +279,12 @@
"detects name collision with an existing but unstable method"
}
declare_lint!
{
pub
UNUSED_LABELS
,
Warn
,
"detects labels that are never used"
}
/// Does nothing as a lint pass, but registers some `Lint`s
/// which are used by other parts of the compiler.
#[derive(Copy,
Clone)]
...
...
@@ -325,6 +331,7 @@ fn get_lints(&self) -> LintArray {
UNUSED_MUT
,
SINGLE_USE_LIFETIME
,
UNUSED_LIFETIME
,
UNUSED_LABELS
,
TYVAR_BEHIND_RAW_POINTER
,
ELIDED_LIFETIME_IN_PATH
,
BARE_TRAIT_OBJECT
,
...
...
src/librustc_resolve/check_unused.rs
浏览文件 @
88f4063c
...
...
@@ -142,6 +142,10 @@ pub fn check_crate(resolver: &mut Resolver, krate: &ast::Crate) {
}
}
for
(
id
,
span
)
in
resolver
.unused_labels
.iter
()
{
resolver
.session
.buffer_lint
(
lint
::
builtin
::
UNUSED_LABELS
,
*
id
,
*
span
,
"unused label"
);
}
let
mut
visitor
=
UnusedImportCheckVisitor
{
resolver
,
unused_imports
:
NodeMap
(),
...
...
src/librustc_resolve/lib.rs
浏览文件 @
88f4063c
...
...
@@ -1473,6 +1473,10 @@ pub struct Resolver<'a> {
pub
maybe_unused_trait_imports
:
NodeSet
,
pub
maybe_unused_extern_crates
:
Vec
<
(
NodeId
,
Span
)
>
,
/// A list of labels as of yet unused. Labels will be removed from this map when
/// they are used (in a `break` or `continue` statement)
pub
unused_labels
:
FxHashMap
<
NodeId
,
Span
>
,
/// privacy errors are delayed until the end in order to deduplicate them
privacy_errors
:
Vec
<
PrivacyError
<
'a
>>
,
/// ambiguity errors are delayed for deduplication
...
...
@@ -1752,6 +1756,8 @@ pub fn new(session: &'a Session,
maybe_unused_trait_imports
:
NodeSet
(),
maybe_unused_extern_crates
:
Vec
::
new
(),
unused_labels
:
FxHashMap
(),
privacy_errors
:
Vec
::
new
(),
ambiguity_errors
:
Vec
::
new
(),
use_injections
:
Vec
::
new
(),
...
...
@@ -3694,6 +3700,7 @@ fn with_resolved_label<F>(&mut self, label: Option<Label>, id: NodeId, f: F)
where
F
:
FnOnce
(
&
mut
Resolver
)
{
if
let
Some
(
label
)
=
label
{
self
.unused_labels
.insert
(
id
,
label
.ident.span
);
let
def
=
Def
::
Label
(
id
);
self
.with_label_rib
(|
this
|
{
this
.label_ribs
.last_mut
()
.unwrap
()
.bindings
.insert
(
label
.ident
,
def
);
...
...
@@ -3742,9 +3749,10 @@ fn resolve_expr(&mut self, expr: &Expr, parent: Option<&Expr>) {
ResolutionError
::
UndeclaredLabel
(
&
label
.ident.name
.as_str
(),
close_match
));
}
Some
(
def
@
Def
::
Label
(
_
))
=>
{
Some
(
Def
::
Label
(
id
))
=>
{
// Since this def is a label, it is never read.
self
.record_def
(
expr
.id
,
PathResolution
::
new
(
def
));
self
.record_def
(
expr
.id
,
PathResolution
::
new
(
Def
::
Label
(
id
)));
self
.unused_labels
.remove
(
&
id
);
}
Some
(
_
)
=>
{
span_bug!
(
expr
.span
,
"label wasn't mapped to a label def!"
);
...
...
src/test/ui/lint/unused_labels.rs
0 → 100644
浏览文件 @
88f4063c
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// The output should warn when a loop label is not used. However, it
// should also deal with the edge cases where a label is shadowed,
// within nested loops
// compile-pass
fn
main
()
{
'unused_while_label
:
while
0
==
0
{
//~^ WARN unused label
}
let
opt
=
Some
(
0
);
'unused_while_let_label
:
while
let
Some
(
_
)
=
opt
{
//~^ WARN unused label
}
'unused_for_label
:
for
_
in
0
..
10
{
//~^ WARN unused label
}
'used_loop_label
:
loop
{
break
'used_loop_label
;
}
'used_loop_label_outer_1
:
for
_
in
0
..
10
{
'used_loop_label_inner_1
:
for
_
in
0
..
10
{
break
'used_loop_label_inner_1
;
}
break
'used_loop_label_outer_1
;
}
'used_loop_label_outer_2
:
for
_
in
0
..
10
{
'unused_loop_label_inner_2
:
for
_
in
0
..
10
{
//~^ WARN unused label
break
'used_loop_label_outer_2
;
}
}
'unused_loop_label_outer_3
:
for
_
in
0
..
10
{
//~^ WARN unused label
'used_loop_label_inner_3
:
for
_
in
0
..
10
{
break
'used_loop_label_inner_3
;
}
}
// Test breaking many times with the same inner label doesn't break the
// warning on the outer label
'many_used_shadowed
:
for
_
in
0
..
10
{
//~^ WARN unused label
'many_used_shadowed
:
for
_
in
0
..
10
{
//~^ WARN label name `'many_used_shadowed` shadows a label name that is already in scope
if
1
%
2
==
0
{
break
'many_used_shadowed
;
}
else
{
break
'many_used_shadowed
;
}
}
}
// This is diverging, so put it at the end so we don't get
// unreachable_code errors everywhere else
'unused_loop_label
:
loop
{
//~^ WARN unused label
}
}
src/test/ui/lint/unused_labels.stderr
0 → 100644
浏览文件 @
88f4063c
warning: unused label
--> $DIR/unused_labels.rs:18:5
|
LL | 'unused_while_label: while 0 == 0 {
| ^^^^^^^^^^^^^^^^^^^
|
= note: #[warn(unused_labels)] on by default
warning: unused label
--> $DIR/unused_labels.rs:23:5
|
LL | 'unused_while_let_label: while let Some(_) = opt {
| ^^^^^^^^^^^^^^^^^^^^^^^
warning: unused label
--> $DIR/unused_labels.rs:27:5
|
LL | 'unused_for_label: for _ in 0..10 {
| ^^^^^^^^^^^^^^^^^
warning: unused label
--> $DIR/unused_labels.rs:43:9
|
LL | 'unused_loop_label_inner_2: for _ in 0..10 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: unused label
--> $DIR/unused_labels.rs:49:5
|
LL | 'unused_loop_label_outer_3: for _ in 0..10 {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: unused label
--> $DIR/unused_labels.rs:58:5
|
LL | 'many_used_shadowed: for _ in 0..10 {
| ^^^^^^^^^^^^^^^^^^^
warning: unused label
--> $DIR/unused_labels.rs:72:5
|
LL | 'unused_loop_label: loop {
| ^^^^^^^^^^^^^^^^^^
warning: label name `'many_used_shadowed` shadows a label name that is already in scope
--> $DIR/unused_labels.rs:60:9
|
LL | 'many_used_shadowed: for _ in 0..10 {
| ------------------- first declared here
LL | //~^ WARN unused label
LL | 'many_used_shadowed: for _ in 0..10 {
| ^^^^^^^^^^^^^^^^^^^ lifetime 'many_used_shadowed already in scope
src/test/ui/loops-reject-duplicate-labels-2.rs
浏览文件 @
88f4063c
...
...
@@ -18,6 +18,7 @@
// discussed here:
// https://internals.rust-lang.org/t/psa-rejecting-duplicate-loop-labels/1833
#[allow(unused_labels)]
pub
fn
foo
()
{
{
'fl
:
for
_
in
0
..
10
{
break
;
}
}
{
'fl
:
loop
{
break
;
}
}
//~ WARN label name `'fl` shadows a label name that is already in scope
...
...
src/test/ui/loops-reject-duplicate-labels-2.stderr
浏览文件 @
88f4063c
warning: label name `'fl` shadows a label name that is already in scope
--> $DIR/loops-reject-duplicate-labels-2.rs:2
3
:7
--> $DIR/loops-reject-duplicate-labels-2.rs:2
4
:7
|
LL | { 'fl: for _ in 0..10 { break; } }
| --- first declared here
...
...
@@ -7,7 +7,7 @@ LL | { 'fl: loop { break; } } //~ WARN label name `'fl` shadows
| ^^^ lifetime 'fl already in scope
warning: label name `'lf` shadows a label name that is already in scope
--> $DIR/loops-reject-duplicate-labels-2.rs:2
5
:7
--> $DIR/loops-reject-duplicate-labels-2.rs:2
6
:7
|
LL | { 'lf: loop { break; } }
| --- first declared here
...
...
@@ -15,7 +15,7 @@ LL | { 'lf: for _ in 0..10 { break; } } //~ WARN label name `'lf` shadows
| ^^^ lifetime 'lf already in scope
warning: label name `'wl` shadows a label name that is already in scope
--> $DIR/loops-reject-duplicate-labels-2.rs:2
7
:7
--> $DIR/loops-reject-duplicate-labels-2.rs:2
8
:7
|
LL | { 'wl: while 2 > 1 { break; } }
| --- first declared here
...
...
@@ -23,7 +23,7 @@ LL | { 'wl: loop { break; } } //~ WARN label name `'wl` shadows
| ^^^ lifetime 'wl already in scope
warning: label name `'lw` shadows a label name that is already in scope
--> $DIR/loops-reject-duplicate-labels-2.rs:
29
:7
--> $DIR/loops-reject-duplicate-labels-2.rs:
30
:7
|
LL | { 'lw: loop { break; } }
| --- first declared here
...
...
@@ -31,7 +31,7 @@ LL | { 'lw: while 2 > 1 { break; } } //~ WARN label name `'lw` shadows
| ^^^ lifetime 'lw already in scope
warning: label name `'fw` shadows a label name that is already in scope
--> $DIR/loops-reject-duplicate-labels-2.rs:3
1
:7
--> $DIR/loops-reject-duplicate-labels-2.rs:3
2
:7
|
LL | { 'fw: for _ in 0..10 { break; } }
| --- first declared here
...
...
@@ -39,7 +39,7 @@ LL | { 'fw: while 2 > 1 { break; } } //~ WARN label name `'fw` shadows
| ^^^ lifetime 'fw already in scope
warning: label name `'wf` shadows a label name that is already in scope
--> $DIR/loops-reject-duplicate-labels-2.rs:3
3
:7
--> $DIR/loops-reject-duplicate-labels-2.rs:3
4
:7
|
LL | { 'wf: while 2 > 1 { break; } }
| --- first declared here
...
...
@@ -47,7 +47,7 @@ LL | { 'wf: for _ in 0..10 { break; } } //~ WARN label name `'wf` shadows
| ^^^ lifetime 'wf already in scope
warning: label name `'tl` shadows a label name that is already in scope
--> $DIR/loops-reject-duplicate-labels-2.rs:3
5
:7
--> $DIR/loops-reject-duplicate-labels-2.rs:3
6
:7
|
LL | { 'tl: while let Some(_) = None::<i32> { break; } }
| --- first declared here
...
...
@@ -55,7 +55,7 @@ LL | { 'tl: loop { break; } } //~ WARN label name `'tl` shadows
| ^^^ lifetime 'tl already in scope
warning: label name `'lt` shadows a label name that is already in scope
--> $DIR/loops-reject-duplicate-labels-2.rs:3
7
:7
--> $DIR/loops-reject-duplicate-labels-2.rs:3
8
:7
|
LL | { 'lt: loop { break; } }
| --- first declared here
...
...
@@ -63,7 +63,7 @@ LL | { 'lt: while let Some(_) = None::<i32> { break; } }
| ^^^ lifetime 'lt already in scope
error: compilation successful
--> $DIR/loops-reject-duplicate-labels-2.rs:4
2
:1
--> $DIR/loops-reject-duplicate-labels-2.rs:4
3
:1
|
LL | / pub fn main() { //~ ERROR compilation successful
LL | | foo();
...
...
src/test/ui/loops-reject-duplicate-labels.rs
浏览文件 @
88f4063c
...
...
@@ -15,6 +15,7 @@
// Issue #21633: reject duplicate loop labels in function bodies.
// This is testing the exact cases that are in the issue description.
#[allow(unused_labels)]
fn
foo
()
{
'fl
:
for
_
in
0
..
10
{
break
;
}
'fl
:
loop
{
break
;
}
//~ WARN label name `'fl` shadows a label name that is already in scope
...
...
src/test/ui/loops-reject-duplicate-labels.stderr
浏览文件 @
88f4063c
warning: label name `'fl` shadows a label name that is already in scope
--> $DIR/loops-reject-duplicate-labels.rs:2
0
:5
--> $DIR/loops-reject-duplicate-labels.rs:2
1
:5
|
LL | 'fl: for _ in 0..10 { break; }
| --- first declared here
...
...
@@ -7,7 +7,7 @@ LL | 'fl: loop { break; } //~ WARN label name `'fl` shadows a labe
| ^^^ lifetime 'fl already in scope
warning: label name `'lf` shadows a label name that is already in scope
--> $DIR/loops-reject-duplicate-labels.rs:2
3
:5
--> $DIR/loops-reject-duplicate-labels.rs:2
4
:5
|
LL | 'lf: loop { break; }
| --- first declared here
...
...
@@ -15,7 +15,7 @@ LL | 'lf: for _ in 0..10 { break; } //~ WARN label name `'lf` shadows a labe
| ^^^ lifetime 'lf already in scope
warning: label name `'wl` shadows a label name that is already in scope
--> $DIR/loops-reject-duplicate-labels.rs:2
5
:5
--> $DIR/loops-reject-duplicate-labels.rs:2
6
:5
|
LL | 'wl: while 2 > 1 { break; }
| --- first declared here
...
...
@@ -23,7 +23,7 @@ LL | 'wl: loop { break; } //~ WARN label name `'wl` shadows a labe
| ^^^ lifetime 'wl already in scope
warning: label name `'lw` shadows a label name that is already in scope
--> $DIR/loops-reject-duplicate-labels.rs:2
7
:5
--> $DIR/loops-reject-duplicate-labels.rs:2
8
:5
|
LL | 'lw: loop { break; }
| --- first declared here
...
...
@@ -31,7 +31,7 @@ LL | 'lw: while 2 > 1 { break; } //~ WARN label name `'lw` shadows a labe
| ^^^ lifetime 'lw already in scope
warning: label name `'fw` shadows a label name that is already in scope
--> $DIR/loops-reject-duplicate-labels.rs:
29
:5
--> $DIR/loops-reject-duplicate-labels.rs:
30
:5
|
LL | 'fw: for _ in 0..10 { break; }
| --- first declared here
...
...
@@ -39,7 +39,7 @@ LL | 'fw: while 2 > 1 { break; } //~ WARN label name `'fw` shadows a labe
| ^^^ lifetime 'fw already in scope
warning: label name `'wf` shadows a label name that is already in scope
--> $DIR/loops-reject-duplicate-labels.rs:3
1
:5
--> $DIR/loops-reject-duplicate-labels.rs:3
2
:5
|
LL | 'wf: while 2 > 1 { break; }
| --- first declared here
...
...
@@ -47,7 +47,7 @@ LL | 'wf: for _ in 0..10 { break; } //~ WARN label name `'wf` shadows a labe
| ^^^ lifetime 'wf already in scope
warning: label name `'tl` shadows a label name that is already in scope
--> $DIR/loops-reject-duplicate-labels.rs:3
3
:5
--> $DIR/loops-reject-duplicate-labels.rs:3
4
:5
|
LL | 'tl: while let Some(_) = None::<i32> { break; }
| --- first declared here
...
...
@@ -55,7 +55,7 @@ LL | 'tl: loop { break; } //~ WARN label name `'tl` shadows a labe
| ^^^ lifetime 'tl already in scope
warning: label name `'lt` shadows a label name that is already in scope
--> $DIR/loops-reject-duplicate-labels.rs:3
5
:5
--> $DIR/loops-reject-duplicate-labels.rs:3
6
:5
|
LL | 'lt: loop { break; }
| --- first declared here
...
...
@@ -63,7 +63,7 @@ LL | 'lt: while let Some(_) = None::<i32> { break; }
| ^^^ lifetime 'lt already in scope
error: compilation successful
--> $DIR/loops-reject-duplicate-labels.rs:
49
:1
--> $DIR/loops-reject-duplicate-labels.rs:
50
:1
|
LL | / pub fn main() { //~ ERROR compilation successful
LL | | let s = S;
...
...
src/test/ui/suggestions/suggest-labels.rs
浏览文件 @
88f4063c
...
...
@@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[allow(unreachable_code)]
#[allow(unreachable_code
,
unused_labels
)]
fn
main
()
{
'foo
:
loop
{
break
'fo
;
//~ ERROR use of undeclared label
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录