-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unreachable-pub lint for
pub
items not reachable from crate root
This is with deepest thanks to Vadim Petrochenkov for thorough review, and resolves #45521.
- Loading branch information
1 parent
e340996
commit 085ec6d
Showing
6 changed files
with
469 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// 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. | ||
|
||
// This is just like unreachable_pub.rs, but without the | ||
// `crate_visibility_modifier` feature (so that we can test the suggestions to | ||
// use `pub(crate)` that are given when that feature is off, as opposed to the | ||
// suggestions to use `crate` given when it is on). When that feature becomes | ||
// stable, this test can be deleted. | ||
|
||
#![feature(macro_vis_matcher)] | ||
|
||
#![allow(unused)] | ||
#![warn(unreachable_pub)] | ||
|
||
mod private_mod { | ||
// non-leaked `pub` items in private module should be linted | ||
pub use std::fmt; | ||
|
||
pub struct Hydrogen { | ||
// `pub` struct fields, too | ||
pub neutrons: usize, | ||
// (... but not more-restricted fields) | ||
pub(crate) electrons: usize | ||
} | ||
impl Hydrogen { | ||
// impls, too | ||
pub fn count_neutrons(&self) -> usize { self.neutrons } | ||
pub(crate) fn count_electrons(&self) -> usize { self.electrons } | ||
} | ||
|
||
pub enum Helium {} | ||
pub union Lithium { c1: usize, c2: u8 } | ||
pub fn beryllium() {} | ||
pub trait Boron {} | ||
pub const CARBON: usize = 1; | ||
pub static NITROGEN: usize = 2; | ||
pub type Oxygen = bool; | ||
|
||
macro_rules! define_empty_struct_with_visibility { | ||
($visibility: vis, $name: ident) => { $visibility struct $name {} } | ||
} | ||
define_empty_struct_with_visibility!(pub, Fluorine); | ||
|
||
extern { | ||
pub fn catalyze() -> bool; | ||
} | ||
|
||
// items leaked through signatures (see `get_neon` below) are OK | ||
pub struct Neon {} | ||
|
||
// crate-visible items are OK | ||
pub(crate) struct Sodium {} | ||
} | ||
|
||
pub mod public_mod { | ||
// module is public: these are OK, too | ||
pub struct Magnesium {} | ||
pub(crate) struct Aluminum {} | ||
} | ||
|
||
pub fn get_neon() -> private_mod::Neon { | ||
private_mod::Neon {} | ||
} | ||
|
||
fn main() { | ||
let _ = get_neon(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
warning: unreachable `pub` item | ||
--> $DIR/unreachable_pub-pub_crate.rs:24:5 | ||
| | ||
24 | pub use std::fmt; | ||
| ---^^^^^^^^^^^^^^ | ||
| | | ||
| help: consider restricting its visibility: `pub(crate)` | ||
| | ||
note: lint level defined here | ||
--> $DIR/unreachable_pub-pub_crate.rs:20:9 | ||
| | ||
20 | #![warn(unreachable_pub)] | ||
| ^^^^^^^^^^^^^^^ | ||
= help: or consider exporting it for use by other crates | ||
|
||
warning: unreachable `pub` item | ||
--> $DIR/unreachable_pub-pub_crate.rs:26:5 | ||
| | ||
26 | pub struct Hydrogen { | ||
| ---^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: consider restricting its visibility: `pub(crate)` | ||
| | ||
= help: or consider exporting it for use by other crates | ||
|
||
warning: unreachable `pub` field | ||
--> $DIR/unreachable_pub-pub_crate.rs:28:9 | ||
| | ||
28 | pub neutrons: usize, | ||
| ---^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: consider restricting its visibility: `pub(crate)` | ||
|
||
warning: unreachable `pub` item | ||
--> $DIR/unreachable_pub-pub_crate.rs:34:9 | ||
| | ||
34 | pub fn count_neutrons(&self) -> usize { self.neutrons } | ||
| ---^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: consider restricting its visibility: `pub(crate)` | ||
|
||
warning: unreachable `pub` item | ||
--> $DIR/unreachable_pub-pub_crate.rs:38:5 | ||
| | ||
38 | pub enum Helium {} | ||
| ---^^^^^^^^^^^^ | ||
| | | ||
| help: consider restricting its visibility: `pub(crate)` | ||
| | ||
= help: or consider exporting it for use by other crates | ||
|
||
warning: unreachable `pub` item | ||
--> $DIR/unreachable_pub-pub_crate.rs:39:5 | ||
| | ||
39 | pub union Lithium { c1: usize, c2: u8 } | ||
| ---^^^^^^^^^^^^^^ | ||
| | | ||
| help: consider restricting its visibility: `pub(crate)` | ||
| | ||
= help: or consider exporting it for use by other crates | ||
|
||
warning: unreachable `pub` item | ||
--> $DIR/unreachable_pub-pub_crate.rs:40:5 | ||
| | ||
40 | pub fn beryllium() {} | ||
| ---^^^^^^^^^^^^^^^ | ||
| | | ||
| help: consider restricting its visibility: `pub(crate)` | ||
| | ||
= help: or consider exporting it for use by other crates | ||
|
||
warning: unreachable `pub` item | ||
--> $DIR/unreachable_pub-pub_crate.rs:41:5 | ||
| | ||
41 | pub trait Boron {} | ||
| ---^^^^^^^^^^^^ | ||
| | | ||
| help: consider restricting its visibility: `pub(crate)` | ||
| | ||
= help: or consider exporting it for use by other crates | ||
|
||
warning: unreachable `pub` item | ||
--> $DIR/unreachable_pub-pub_crate.rs:42:5 | ||
| | ||
42 | pub const CARBON: usize = 1; | ||
| ---^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: consider restricting its visibility: `pub(crate)` | ||
| | ||
= help: or consider exporting it for use by other crates | ||
|
||
warning: unreachable `pub` item | ||
--> $DIR/unreachable_pub-pub_crate.rs:43:5 | ||
| | ||
43 | pub static NITROGEN: usize = 2; | ||
| ---^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: consider restricting its visibility: `pub(crate)` | ||
| | ||
= help: or consider exporting it for use by other crates | ||
|
||
warning: unreachable `pub` item | ||
--> $DIR/unreachable_pub-pub_crate.rs:44:5 | ||
| | ||
44 | pub type Oxygen = bool; | ||
| ---^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: consider restricting its visibility: `pub(crate)` | ||
| | ||
= help: or consider exporting it for use by other crates | ||
|
||
warning: unreachable `pub` item | ||
--> $DIR/unreachable_pub-pub_crate.rs:47:47 | ||
| | ||
47 | ($visibility: vis, $name: ident) => { $visibility struct $name {} } | ||
| -----------^^^^^^^^^^^^^ | ||
| | | ||
| help: consider restricting its visibility: `pub(crate)` | ||
48 | } | ||
49 | define_empty_struct_with_visibility!(pub, Fluorine); | ||
| ---------------------------------------------------- in this macro invocation | ||
| | ||
= help: or consider exporting it for use by other crates | ||
|
||
warning: unreachable `pub` item | ||
--> $DIR/unreachable_pub-pub_crate.rs:52:9 | ||
| | ||
52 | pub fn catalyze() -> bool; | ||
| ---^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: consider restricting its visibility: `pub(crate)` | ||
| | ||
= help: or consider exporting it for use by other crates | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// 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. | ||
|
||
#![feature(crate_visibility_modifier)] | ||
#![feature(macro_vis_matcher)] | ||
|
||
#![allow(unused)] | ||
#![warn(unreachable_pub)] | ||
|
||
mod private_mod { | ||
// non-leaked `pub` items in private module should be linted | ||
pub use std::fmt; | ||
|
||
pub struct Hydrogen { | ||
// `pub` struct fields, too | ||
pub neutrons: usize, | ||
// (... but not more-restricted fields) | ||
crate electrons: usize | ||
} | ||
impl Hydrogen { | ||
// impls, too | ||
pub fn count_neutrons(&self) -> usize { self.neutrons } | ||
crate fn count_electrons(&self) -> usize { self.electrons } | ||
} | ||
|
||
pub enum Helium {} | ||
pub union Lithium { c1: usize, c2: u8 } | ||
pub fn beryllium() {} | ||
pub trait Boron {} | ||
pub const CARBON: usize = 1; | ||
pub static NITROGEN: usize = 2; | ||
pub type Oxygen = bool; | ||
|
||
macro_rules! define_empty_struct_with_visibility { | ||
($visibility: vis, $name: ident) => { $visibility struct $name {} } | ||
} | ||
define_empty_struct_with_visibility!(pub, Fluorine); | ||
|
||
extern { | ||
pub fn catalyze() -> bool; | ||
} | ||
|
||
// items leaked through signatures (see `get_neon` below) are OK | ||
pub struct Neon {} | ||
|
||
// crate-visible items are OK | ||
crate struct Sodium {} | ||
} | ||
|
||
pub mod public_mod { | ||
// module is public: these are OK, too | ||
pub struct Magnesium {} | ||
crate struct Aluminum {} | ||
} | ||
|
||
pub fn get_neon() -> private_mod::Neon { | ||
private_mod::Neon {} | ||
} | ||
|
||
fn main() { | ||
let _ = get_neon(); | ||
} |
Oops, something went wrong.