-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unreachable-pub
lint (as authorized by RFC 2126)
#45569
Merged
+469
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a test without this feature enabled to make sure there're no regressions going forward with the
pub(crate)
suggestion?