File: unreachable_pub.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (116 lines) | stat: -rw-r--r-- 3,388 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//@ check-pass
//@ edition: 2018
//@ run-rustfix

#![allow(unused)]
#![warn(unreachable_pub)]

mod private_mod {
    // non-leaked `pub` items in private module should be linted
    pub use std::fmt; //~ WARNING unreachable_pub
    pub use std::env::{Args}; // braced-use has different item spans than unbraced
    //~^ WARNING unreachable_pub

    // we lint on struct definition
    pub struct Hydrogen { //~ WARNING unreachable_pub
        // but not on fields, even if they are `pub` as putting `pub(crate)`
        // it would clutter the source code for little value
        pub neutrons: usize,
        pub(crate) electrons: usize
    }
    pub(crate) struct Calcium {
        pub neutrons: usize,
    }
    impl Hydrogen {
        // impls, too
        pub fn count_neutrons(&self) -> usize { self.neutrons } //~ WARNING unreachable_pub
        pub(crate) fn count_electrons(&self) -> usize { self.electrons }
    }
    impl Clone for Hydrogen {
        fn clone(&self) -> Hydrogen {
            Hydrogen { neutrons: self.neutrons, electrons: self.electrons }
        }
    }

    pub enum Helium {} //~ WARNING unreachable_pub
    pub union Lithium { c1: usize, c2: u8 } //~ WARNING unreachable_pub
    pub fn beryllium() {} //~ WARNING unreachable_pub
    pub trait Boron {} //~ WARNING unreachable_pub
    pub const CARBON: usize = 1; //~ WARNING unreachable_pub
    pub static NITROGEN: usize = 2; //~ WARNING unreachable_pub
    pub type Oxygen = bool; //~ WARNING unreachable_pub

    macro_rules! define_empty_struct_with_visibility {
        ($visibility: vis, $name: ident) => { $visibility struct $name {} }
        //~^ WARNING unreachable_pub
    }
    define_empty_struct_with_visibility!(pub, Fluorine);

    extern "C" {
        pub fn catalyze() -> bool; //~ WARNING unreachable_pub
    }

    mod private_in_private {
        pub enum Helium {} //~ WARNING unreachable_pub
        pub fn beryllium() {} //~ WARNING unreachable_pub
    }

    pub(crate) mod crate_in_private {
        pub const CARBON: usize = 1; //~ WARNING unreachable_pub
    }

    pub mod pub_in_private { //~ WARNING unreachable_pub
        pub static NITROGEN: usize = 2; //~ WARNING unreachable_pub
    }

    fn foo() {
        const {
            pub struct Foo; //~ WARNING unreachable_pub
        };
    }

    enum Weird {
        Variant = {
            pub struct Foo; //~ WARNING unreachable_pub

            mod tmp {
                pub struct Bar; //~ WARNING unreachable_pub
            }

            let _ = tmp::Bar;

            0
        },
    }

    pub use fpu_precision::set_precision; //~ WARNING unreachable_pub

    mod fpu_precision {
        pub fn set_precision<T>() {} //~ WARNING unreachable_pub
        pub fn set_micro_precision<T>() {} //~ WARNING unreachable_pub
    }

    // 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();
    let _ = private_mod::beryllium();
    let _ = private_mod::crate_in_private::CARBON;
    let _ = private_mod::pub_in_private::NITROGEN;
    let _ = unsafe { private_mod::catalyze() };
}