Skip to content

Commit 66d253f

Browse files
committed
pub_use
1 parent 5c19ae9 commit 66d253f

File tree

7 files changed

+86
-0
lines changed

7 files changed

+86
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3527,6 +3527,7 @@ Released 2018-09-13
35273527
[`ptr_eq`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_eq
35283528
[`ptr_offset_with_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#ptr_offset_with_cast
35293529
[`pub_enum_variant_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#pub_enum_variant_names
3530+
[`pub_use`]: https://rust-lang.github.io/rust-clippy/master/index.html#pub_use
35303531
[`question_mark`]: https://rust-lang.github.io/rust-clippy/master/index.html#question_mark
35313532
[`range_minus_one`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_minus_one
35323533
[`range_plus_one`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_plus_one

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ store.register_lints(&[
432432
ptr::PTR_ARG,
433433
ptr_eq::PTR_EQ,
434434
ptr_offset_with_cast::PTR_OFFSET_WITH_CAST,
435+
pub_use::PUB_USE,
435436
question_mark::QUESTION_MARK,
436437
ranges::MANUAL_RANGE_CONTAINS,
437438
ranges::RANGE_MINUS_ONE,

clippy_lints/src/lib.register_restriction.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
5353
LintId::of(panic_unimplemented::UNIMPLEMENTED),
5454
LintId::of(panic_unimplemented::UNREACHABLE),
5555
LintId::of(pattern_type_mismatch::PATTERN_TYPE_MISMATCH),
56+
LintId::of(pub_use::PUB_USE),
5657
LintId::of(redundant_slicing::DEREF_BY_SLICING),
5758
LintId::of(same_name_method::SAME_NAME_METHOD),
5859
LintId::of(shadow::SHADOW_REUSE),

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ mod precedence;
336336
mod ptr;
337337
mod ptr_eq;
338338
mod ptr_offset_with_cast;
339+
mod pub_use;
339340
mod question_mark;
340341
mod ranges;
341342
mod redundant_clone;
@@ -870,6 +871,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
870871
store.register_early_pass(|| Box::new(crate_in_macro_def::CrateInMacroDef));
871872
store.register_early_pass(|| Box::new(empty_structs_with_brackets::EmptyStructsWithBrackets));
872873
store.register_late_pass(|| Box::new(unnecessary_owned_empty_strings::UnnecessaryOwnedEmptyStrings));
874+
store.register_early_pass(|| Box::new(pub_use::PubUse));
873875
// add lints here, do not remove this comment, it's used in `new_lint`
874876
}
875877

clippy_lints/src/pub_use.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
use rustc_ast::ast::{Item, ItemKind, VisibilityKind};
3+
use rustc_lint::{EarlyContext, EarlyLintPass};
4+
use rustc_session::{declare_lint_pass, declare_tool_lint};
5+
6+
declare_clippy_lint! {
7+
/// ### What it does
8+
///
9+
/// Restricts the usage of `pub use ...`
10+
///
11+
/// ### Why is this bad?
12+
///
13+
/// `pub use` is usually fine, but a project may wish to limit `pub use` instances to prevent
14+
/// unintentional exports or to encourage placing exported items directly in public modules
15+
///
16+
/// ### Example
17+
/// ```rust
18+
/// pub mod outer {
19+
/// mod inner {
20+
/// pub struct Test {}
21+
/// }
22+
/// pub use inner::Test;
23+
/// }
24+
///
25+
/// use outer::Test;
26+
/// ```
27+
/// Use instead:
28+
/// ```rust
29+
/// pub mod outer {
30+
/// pub struct Test {}
31+
/// }
32+
///
33+
/// use outer::Test;
34+
/// ```
35+
#[clippy::version = "1.62.0"]
36+
pub PUB_USE,
37+
restriction,
38+
"restricts the usage of `pub use`"
39+
}
40+
declare_lint_pass!(PubUse => [PUB_USE]);
41+
42+
impl EarlyLintPass for PubUse {
43+
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
44+
if let ItemKind::Use(_) = item.kind &&
45+
let VisibilityKind::Public = item.vis.kind {
46+
span_lint_and_help(
47+
cx,
48+
PUB_USE,
49+
item.span,
50+
"using `pub use`",
51+
None,
52+
"move the exported item to a public module instead",
53+
);
54+
}
55+
}
56+
}

tests/ui/pub_use.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![warn(clippy::pub_use)]
2+
#![allow(unused_imports)]
3+
#![no_main]
4+
5+
pub mod outer {
6+
mod inner {
7+
pub struct Test {}
8+
}
9+
// should be linted
10+
pub use inner::Test;
11+
}
12+
13+
// should not be linted
14+
use std::fmt;

tests/ui/pub_use.stderr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: using `pub use`
2+
--> $DIR/pub_use.rs:10:5
3+
|
4+
LL | pub use inner::Test;
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::pub-use` implied by `-D warnings`
8+
= help: move the exported item to a public module instead
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)