|
| 1 | +//@ check-pass |
| 2 | +//@ edition: 2018 |
| 3 | +//@ run-rustfix |
| 4 | + |
| 5 | +#![allow(unused)] |
| 6 | +#![warn(unreachable_pub)] |
| 7 | + |
| 8 | +mod private_mod { |
| 9 | + // non-leaked `pub` items in private module should be linted |
| 10 | + pub(crate) use std::fmt; //~ WARNING unreachable_pub |
| 11 | + pub(crate) use std::env::{Args}; // braced-use has different item spans than unbraced |
| 12 | + //~^ WARNING unreachable_pub |
| 13 | + |
| 14 | + // we lint on struct definition |
| 15 | + pub(crate) struct Hydrogen { //~ WARNING unreachable_pub |
| 16 | + // but not on fields, even if they are `pub` as putting `pub(crate)` |
| 17 | + // it would clutter the source code for little value |
| 18 | + pub neutrons: usize, |
| 19 | + pub(crate) electrons: usize |
| 20 | + } |
| 21 | + pub(crate) struct Calcium { |
| 22 | + pub neutrons: usize, |
| 23 | + } |
| 24 | + impl Hydrogen { |
| 25 | + // impls, too |
| 26 | + pub(crate) fn count_neutrons(&self) -> usize { self.neutrons } //~ WARNING unreachable_pub |
| 27 | + pub(crate) fn count_electrons(&self) -> usize { self.electrons } |
| 28 | + } |
| 29 | + impl Clone for Hydrogen { |
| 30 | + fn clone(&self) -> Hydrogen { |
| 31 | + Hydrogen { neutrons: self.neutrons, electrons: self.electrons } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + pub(crate) enum Helium {} //~ WARNING unreachable_pub |
| 36 | + pub(crate) union Lithium { c1: usize, c2: u8 } //~ WARNING unreachable_pub |
| 37 | + pub(crate) fn beryllium() {} //~ WARNING unreachable_pub |
| 38 | + pub(crate) trait Boron {} //~ WARNING unreachable_pub |
| 39 | + pub(crate) const CARBON: usize = 1; //~ WARNING unreachable_pub |
| 40 | + pub(crate) static NITROGEN: usize = 2; //~ WARNING unreachable_pub |
| 41 | + pub(crate) type Oxygen = bool; //~ WARNING unreachable_pub |
| 42 | + |
| 43 | + macro_rules! define_empty_struct_with_visibility { |
| 44 | + ($visibility: vis, $name: ident) => { $visibility struct $name {} } |
| 45 | + //~^ WARNING unreachable_pub |
| 46 | + } |
| 47 | + define_empty_struct_with_visibility!(pub(crate), Fluorine); |
| 48 | + |
| 49 | + extern "C" { |
| 50 | + pub(crate) fn catalyze() -> bool; //~ WARNING unreachable_pub |
| 51 | + } |
| 52 | + |
| 53 | + mod private_in_private { |
| 54 | + pub(super) enum Helium {} //~ WARNING unreachable_pub |
| 55 | + pub(super) fn beryllium() {} //~ WARNING unreachable_pub |
| 56 | + } |
| 57 | + |
| 58 | + pub(crate) mod crate_in_private { |
| 59 | + pub(crate) const CARBON: usize = 1; //~ WARNING unreachable_pub |
| 60 | + } |
| 61 | + |
| 62 | + pub(crate) mod pub_in_private { //~ WARNING unreachable_pub |
| 63 | + pub(crate) static NITROGEN: usize = 2; //~ WARNING unreachable_pub |
| 64 | + } |
| 65 | + |
| 66 | + fn foo() { |
| 67 | + const { |
| 68 | + pub(crate) struct Foo; //~ WARNING unreachable_pub |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + enum Weird { |
| 73 | + Variant = { |
| 74 | + pub(crate) struct Foo; //~ WARNING unreachable_pub |
| 75 | + |
| 76 | + mod tmp { |
| 77 | + pub(crate) struct Bar; //~ WARNING unreachable_pub |
| 78 | + } |
| 79 | + |
| 80 | + let _ = tmp::Bar; |
| 81 | + |
| 82 | + 0 |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + pub(crate) use fpu_precision::set_precision; //~ WARNING unreachable_pub |
| 87 | + |
| 88 | + mod fpu_precision { |
| 89 | + pub(crate) fn set_precision<T>() {} //~ WARNING unreachable_pub |
| 90 | + pub(super) fn set_micro_precision<T>() {} //~ WARNING unreachable_pub |
| 91 | + } |
| 92 | + |
| 93 | + // items leaked through signatures (see `get_neon` below) are OK |
| 94 | + pub struct Neon {} |
| 95 | + |
| 96 | + // crate-visible items are OK |
| 97 | + pub(crate) struct Sodium {} |
| 98 | +} |
| 99 | + |
| 100 | +pub mod public_mod { |
| 101 | + // module is public: these are OK, too |
| 102 | + pub struct Magnesium {} |
| 103 | + pub(crate) struct Aluminum {} |
| 104 | +} |
| 105 | + |
| 106 | +pub fn get_neon() -> private_mod::Neon { |
| 107 | + private_mod::Neon {} |
| 108 | +} |
| 109 | + |
| 110 | +fn main() { |
| 111 | + let _ = get_neon(); |
| 112 | + let _ = private_mod::beryllium(); |
| 113 | + let _ = private_mod::crate_in_private::CARBON; |
| 114 | + let _ = private_mod::pub_in_private::NITROGEN; |
| 115 | + let _ = unsafe { private_mod::catalyze() }; |
| 116 | +} |
0 commit comments