Skip to content

Commit

Permalink
Rollup merge of rust-lang#47726 - pietroalbini:fix-nested-empty-group…
Browse files Browse the repository at this point in the history
…s-span, r=petrochenkov

Fix spans in unused import lint for nested groups

This fixes an inconsistency for empty nested groups, and adds a test for all the possible cases of the lint.

```
warning: unused imports: `*`, `Foo`, `baz::{}`, `foobar::*`
  --> test.rs:16:11
   |
16 | use foo::{Foo, bar::{baz::{}, foobar::*}, *};
   |           ^^^        ^^^^^^^  ^^^^^^^^^   ^
   |
   = note: #[warn(unused_imports)] on by default

warning: unused import: `*`
  --> test.rs:17:24
   |
17 | use foo::bar::baz::{*, *};
   |                        ^

warning: unused import: `use foo::{};`
  --> test.rs:18:1
   |
18 | use foo::{};
   | ^^^^^^^^^^^^
```

cc rust-lang#44494
  • Loading branch information
GuillaumeGomez authored Jan 25, 2018
2 parents 6f2a0c6 + 0847ac0 commit 58e56cc
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
9 changes: 8 additions & 1 deletion src/librustc_resolve/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,18 @@ impl<'a, 'b> Visitor<'a> for UnusedImportCheckVisitor<'a, 'b> {
}

if let ast::UseTreeKind::Nested(ref items) = use_tree.kind {
// If it's the parent group, cover the entire use item
let span = if nested {
use_tree.span
} else {
self.item_span
};

if items.len() == 0 {
self.unused_imports
.entry(self.base_id)
.or_insert_with(NodeMap)
.insert(id, self.item_span);
.insert(id, span);
}
} else {
let base_id = self.base_id;
Expand Down
14 changes: 0 additions & 14 deletions src/test/ui/owl-import-generates-unused-import-lint.stderr

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
Expand All @@ -9,13 +9,26 @@
// except according to those terms.

#![feature(use_nested_groups)]
#![allow(dead_code)]
#![deny(unused_imports)]

mod foo {
pub enum Bar {}
pub mod bar {
pub mod baz {
pub struct Bar();
}
pub mod foobar {}
}

pub struct Foo();
}

use foo::{*, *}; //~ ERROR unused import: `*`
use foo::{Foo, bar::{baz::{}, foobar::*}, *};
//~^ ERROR unused imports: `*`, `Foo`, `baz::{}`, `foobar::*`
use foo::bar::baz::{*, *};
//~^ ERROR unused import: `*`
use foo::{};
//~^ ERROR unused import: `use foo::{};`

fn main() {
let _: Bar;
Expand Down
26 changes: 26 additions & 0 deletions src/test/ui/use-nested-groups-unused-imports.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error: unused imports: `*`, `Foo`, `baz::{}`, `foobar::*`
--> $DIR/use-nested-groups-unused-imports.rs:26:11
|
26 | use foo::{Foo, bar::{baz::{}, foobar::*}, *};
| ^^^ ^^^^^^^ ^^^^^^^^^ ^
|
note: lint level defined here
--> $DIR/use-nested-groups-unused-imports.rs:13:9
|
13 | #![deny(unused_imports)]
| ^^^^^^^^^^^^^^

error: unused import: `*`
--> $DIR/use-nested-groups-unused-imports.rs:28:24
|
28 | use foo::bar::baz::{*, *};
| ^

error: unused import: `use foo::{};`
--> $DIR/use-nested-groups-unused-imports.rs:30:1
|
30 | use foo::{};
| ^^^^^^^^^^^^

error: aborting due to 3 previous errors

0 comments on commit 58e56cc

Please sign in to comment.