-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathvisibility.rs
149 lines (144 loc) · 5.05 KB
/
visibility.rs
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::SpanRangeExt;
use rustc_ast::ast::{Item, VisibilityKind};
use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_session::declare_lint_pass;
use rustc_span::Span;
use rustc_span::symbol::kw;
declare_clippy_lint! {
/// ### What it does
/// Checks for usage of `pub(self)` and `pub(in self)`.
///
/// ### Why is this bad?
/// It's unnecessary, omitting the `pub` entirely will give the same results.
///
/// ### Example
/// ```rust,ignore
/// pub(self) type OptBox<T> = Option<Box<T>>;
/// ```
/// Use instead:
/// ```rust,ignore
/// type OptBox<T> = Option<Box<T>>;
/// ```
#[clippy::version = "1.72.0"]
pub NEEDLESS_PUB_SELF,
style,
"checks for usage of `pub(self)` and `pub(in self)`."
}
declare_clippy_lint! {
/// ### What it does
/// Checks for usage of `pub(<loc>)` with `in`.
///
/// ### Why restrict this?
/// Consistency. Use it or don't, just be consistent about it.
///
/// Also see the `pub_without_shorthand` lint for an alternative.
///
/// ### Example
/// ```rust,ignore
/// pub(super) type OptBox<T> = Option<Box<T>>;
/// ```
/// Use instead:
/// ```rust,ignore
/// pub(in super) type OptBox<T> = Option<Box<T>>;
/// ```
#[clippy::version = "1.72.0"]
pub PUB_WITH_SHORTHAND,
restriction,
"disallows usage of `pub(<loc>)`, without `in`"
}
declare_clippy_lint! {
/// ### What it does
/// Checks for usage of `pub(<loc>)` without `in`.
///
/// Note: As you cannot write a module's path in `pub(<loc>)`, this will only trigger on
/// `pub(super)` and the like.
///
/// ### Why restrict this?
/// Consistency. Use it or don't, just be consistent about it.
///
/// Also see the `pub_with_shorthand` lint for an alternative.
///
/// ### Example
/// ```rust,ignore
/// pub(in super) type OptBox<T> = Option<Box<T>>;
/// ```
/// Use instead:
/// ```rust,ignore
/// pub(super) type OptBox<T> = Option<Box<T>>;
/// ```
#[clippy::version = "1.72.0"]
pub PUB_WITHOUT_SHORTHAND,
restriction,
"disallows usage of `pub(in <loc>)` with `in`"
}
declare_lint_pass!(Visibility => [NEEDLESS_PUB_SELF, PUB_WITH_SHORTHAND, PUB_WITHOUT_SHORTHAND]);
impl EarlyLintPass for Visibility {
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
if !item.span.in_external_macro(cx.sess().source_map())
&& let VisibilityKind::Restricted { path, shorthand, .. } = &item.vis.kind
{
if **path == kw::SelfLower && !is_from_proc_macro(cx, item.vis.span) {
span_lint_and_then(
cx,
NEEDLESS_PUB_SELF,
item.vis.span,
format!("unnecessary `pub({}self)`", if *shorthand { "" } else { "in " }),
|diag| {
diag.span_suggestion_hidden(
item.vis.span,
"remove it",
String::new(),
Applicability::MachineApplicable,
);
},
);
}
if (**path == kw::Super || **path == kw::SelfLower || **path == kw::Crate)
&& !*shorthand
&& let [.., last] = &*path.segments
&& !is_from_proc_macro(cx, item.vis.span)
{
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
PUB_WITHOUT_SHORTHAND,
item.vis.span,
"usage of `pub` with `in`",
|diag| {
diag.span_suggestion(
item.vis.span,
"remove it",
format!("pub({})", last.ident),
Applicability::MachineApplicable,
);
},
);
}
if *shorthand
&& let [.., last] = &*path.segments
&& !is_from_proc_macro(cx, item.vis.span)
{
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
PUB_WITH_SHORTHAND,
item.vis.span,
"usage of `pub` without `in`",
|diag| {
diag.span_suggestion(
item.vis.span,
"add it",
format!("pub(in {})", last.ident),
Applicability::MachineApplicable,
);
},
);
}
}
}
}
fn is_from_proc_macro(cx: &EarlyContext<'_>, span: Span) -> bool {
!span.check_source_text(cx, |src| src.starts_with("pub"))
}