forked from rust-lang/rust-clippy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
427 additions
and
2 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
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,127 @@ | ||
use clippy_utils::{diagnostics::span_lint_and_sugg, source::snippet_opt}; | ||
use rustc_ast::ast::{Item, VisibilityKind}; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::{symbol::kw, Span}; | ||
|
||
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 | ||
/// pub(self) type OptBox<T> = Option<Box<T>>; | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// type OptBox<T> = Option<Box<T>>; | ||
/// ``` | ||
#[clippy::version = "1.72.0"] | ||
pub NEEDLESS_PUB_SELF, | ||
complexity, | ||
"checks for usage of `pub(self)` and `pub(in self)`." | ||
} | ||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for missing usage of the `pub(in <loc>)` shorthand. | ||
/// | ||
/// ### Why is this bad? | ||
/// Consistency. Use it or don't, just be consistent about it. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// pub(super) type OptBox<T> = Option<Box<T>>; | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// pub(in super) type OptBox<T> = Option<Box<T>>; | ||
/// ``` | ||
#[clippy::version = "1.72.0"] | ||
pub PUB_WITH_SHORTHAND, | ||
restriction, | ||
"disallows usage of the `pub(<loc>)`, suggesting use of the `in` shorthand" | ||
} | ||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for usage of the `pub(in <loc>)` shorthand. | ||
/// | ||
/// Note: As you cannot write a module's path in `pub(<loc>)`, this will only trigger on | ||
/// `pub(super)` and the like. | ||
/// | ||
/// ### Why is this bad? | ||
/// Consistency. Use it or don't, just be consistent about it. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// pub(in super) type OptBox<T> = Option<Box<T>>; | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// pub(super) type OptBox<T> = Option<Box<T>>; | ||
/// ``` | ||
#[clippy::version = "1.72.0"] | ||
pub PUB_WITHOUT_SHORTHAND, | ||
restriction, | ||
"disallows usage of the `pub(in <loc>)` shorthand wherever possible" | ||
} | ||
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 !in_external_macro(cx.sess(), item.span) | ||
&& let VisibilityKind::Restricted { path, shorthand, .. } = &item.vis.kind | ||
{ | ||
if **path == kw::SelfLower && let Some(false) = is_from_proc_macro(cx, item.vis.span) { | ||
span_lint_and_sugg( | ||
cx, | ||
NEEDLESS_PUB_SELF, | ||
item.vis.span, | ||
&format!("unnecessary `pub({}self)`", if *shorthand { "" } else { "in " }), | ||
"remove it", | ||
String::new(), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
|
||
if (**path == kw::Super || **path == kw::SelfLower || **path == kw::Crate) | ||
&& !*shorthand | ||
&& let [.., last] = &*path.segments | ||
&& let Some(false) = is_from_proc_macro(cx, item.vis.span) | ||
{ | ||
span_lint_and_sugg( | ||
cx, | ||
PUB_WITHOUT_SHORTHAND, | ||
item.vis.span, | ||
"usage of `pub` with `in`", | ||
"remove it", | ||
format!("pub({})", last.ident), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
|
||
if *shorthand | ||
&& let [.., last] = &*path.segments | ||
&& let Some(false) = is_from_proc_macro(cx, item.vis.span) | ||
{ | ||
span_lint_and_sugg( | ||
cx, | ||
PUB_WITH_SHORTHAND, | ||
item.vis.span, | ||
"usage of `pub` without `in`", | ||
"add it", | ||
format!("pub(in {})", last.ident), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn is_from_proc_macro(cx: &EarlyContext<'_>, span: Span) -> Option<bool> { | ||
snippet_opt(cx, span).map(|s| !s.starts_with("pub")) | ||
} |
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,33 @@ | ||
//@run-rustfix | ||
//@aux-build:proc_macros.rs | ||
#![feature(custom_inner_attributes)] | ||
#![allow(unused)] | ||
#![warn(clippy::needless_pub_self)] | ||
#![no_main] | ||
#![rustfmt::skip] // rustfmt will remove `in`, understandable | ||
// but very annoying for our purposes! | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
fn a() {} | ||
fn b() {} | ||
|
||
pub fn c() {} | ||
mod a { | ||
pub(in super) fn d() {} | ||
pub(super) fn e() {} | ||
fn f() {} | ||
} | ||
|
||
external! { | ||
pub(self) fn g() {} | ||
pub(in self) fn h() {} | ||
} | ||
with_span! { | ||
span | ||
pub(self) fn i() {} | ||
pub(in self) fn j() {} | ||
} | ||
|
||
// not really anything more to test. just a really simple lint overall |
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,33 @@ | ||
//@run-rustfix | ||
//@aux-build:proc_macros.rs | ||
#![feature(custom_inner_attributes)] | ||
#![allow(unused)] | ||
#![warn(clippy::needless_pub_self)] | ||
#![no_main] | ||
#![rustfmt::skip] // rustfmt will remove `in`, understandable | ||
// but very annoying for our purposes! | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
pub(self) fn a() {} | ||
pub(in self) fn b() {} | ||
|
||
pub fn c() {} | ||
mod a { | ||
pub(in super) fn d() {} | ||
pub(super) fn e() {} | ||
pub(self) fn f() {} | ||
} | ||
|
||
external! { | ||
pub(self) fn g() {} | ||
pub(in self) fn h() {} | ||
} | ||
with_span! { | ||
span | ||
pub(self) fn i() {} | ||
pub(in self) fn j() {} | ||
} | ||
|
||
// not really anything more to test. just a really simple lint overall |
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,22 @@ | ||
error: unnecessary `pub(self)` | ||
--> $DIR/needless_pub_self.rs:13:1 | ||
| | ||
LL | pub(self) fn a() {} | ||
| ^^^^^^^^^ help: remove it | ||
| | ||
= note: `-D clippy::needless-pub-self` implied by `-D warnings` | ||
|
||
error: unnecessary `pub(in self)` | ||
--> $DIR/needless_pub_self.rs:14:1 | ||
| | ||
LL | pub(in self) fn b() {} | ||
| ^^^^^^^^^^^^ help: remove it | ||
|
||
error: unnecessary `pub(self)` | ||
--> $DIR/needless_pub_self.rs:20:5 | ||
| | ||
LL | pub(self) fn f() {} | ||
| ^^^^^^^^^ help: remove it | ||
|
||
error: aborting due to 3 previous errors | ||
|
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,38 @@ | ||
//@run-rustfix | ||
//@aux-build:proc_macros.rs | ||
#![feature(custom_inner_attributes)] | ||
#![allow(clippy::needless_pub_self, unused)] | ||
#![warn(clippy::pub_with_shorthand)] | ||
#![no_main] | ||
#![rustfmt::skip] // rustfmt will remove `in`, understandable | ||
// but very annoying for our purposes! | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
pub(in self) fn a() {} | ||
pub(in self) fn b() {} | ||
|
||
pub fn c() {} | ||
mod a { | ||
pub(in super) fn d() {} | ||
pub(in super) fn e() {} | ||
pub(in self) fn f() {} | ||
pub(in crate) fn k() {} | ||
pub(in crate) fn m() {} | ||
mod b { | ||
pub(in crate::a) fn l() {} | ||
} | ||
} | ||
|
||
external! { | ||
pub(self) fn g() {} | ||
pub(in self) fn h() {} | ||
} | ||
with_span! { | ||
span | ||
pub(self) fn i() {} | ||
pub(in self) fn j() {} | ||
} | ||
|
||
// not really anything more to test. just a really simple lint overall |
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,38 @@ | ||
//@run-rustfix | ||
//@aux-build:proc_macros.rs | ||
#![feature(custom_inner_attributes)] | ||
#![allow(clippy::needless_pub_self, unused)] | ||
#![warn(clippy::pub_with_shorthand)] | ||
#![no_main] | ||
#![rustfmt::skip] // rustfmt will remove `in`, understandable | ||
// but very annoying for our purposes! | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
pub(self) fn a() {} | ||
pub(in self) fn b() {} | ||
|
||
pub fn c() {} | ||
mod a { | ||
pub(in super) fn d() {} | ||
pub(super) fn e() {} | ||
pub(self) fn f() {} | ||
pub(crate) fn k() {} | ||
pub(in crate) fn m() {} | ||
mod b { | ||
pub(in crate::a) fn l() {} | ||
} | ||
} | ||
|
||
external! { | ||
pub(self) fn g() {} | ||
pub(in self) fn h() {} | ||
} | ||
with_span! { | ||
span | ||
pub(self) fn i() {} | ||
pub(in self) fn j() {} | ||
} | ||
|
||
// not really anything more to test. just a really simple lint overall |
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,28 @@ | ||
error: usage of `pub` without `in` | ||
--> $DIR/pub_with_shorthand.rs:13:1 | ||
| | ||
LL | pub(self) fn a() {} | ||
| ^^^^^^^^^ help: add it: `pub(in self)` | ||
| | ||
= note: `-D clippy::pub-with-shorthand` implied by `-D warnings` | ||
|
||
error: usage of `pub` without `in` | ||
--> $DIR/pub_with_shorthand.rs:19:5 | ||
| | ||
LL | pub(super) fn e() {} | ||
| ^^^^^^^^^^ help: add it: `pub(in super)` | ||
|
||
error: usage of `pub` without `in` | ||
--> $DIR/pub_with_shorthand.rs:20:5 | ||
| | ||
LL | pub(self) fn f() {} | ||
| ^^^^^^^^^ help: add it: `pub(in self)` | ||
|
||
error: usage of `pub` without `in` | ||
--> $DIR/pub_with_shorthand.rs:21:5 | ||
| | ||
LL | pub(crate) fn k() {} | ||
| ^^^^^^^^^^ help: add it: `pub(in crate)` | ||
|
||
error: aborting due to 4 previous errors | ||
|
Oops, something went wrong.