-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Fixes #9677 - Lint against mod lib; #9708
Closed
Closed
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,52 @@ | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use rustc_ast::{ptr::P, Crate, Item, ItemKind, ModKind}; | ||
use rustc_lint::{EarlyContext, EarlyLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::sym; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// // example code where clippy issues a warning | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// // example code which does not raise clippy warning | ||
/// ``` | ||
#[clippy::version = "1.66.0"] | ||
pub MOD_LIB, | ||
pedantic, | ||
"default lint description" | ||
} | ||
declare_lint_pass!(ModLib => [MOD_LIB]); | ||
|
||
impl EarlyLintPass for ModLib { | ||
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) { | ||
// println!("MOO Checking crate {:#?}", krate); | ||
check_mod(cx, &krate.items); | ||
} | ||
} | ||
|
||
fn check_mod(cx: &EarlyContext<'_>, items: &[P<Item>]) { | ||
for item in items { | ||
if let ItemKind::Mod(_, ModKind::Loaded(..)) = item.kind { | ||
// println!("MOO Got a Mod: {:#?}", items); | ||
// println!("MOO Got a Mod: {:#?}", item.ident.name); | ||
|
||
if item.ident.name == sym::lib { | ||
span_lint_and_help( | ||
cx, | ||
MOD_LIB, | ||
item.span, | ||
"uncommon use of mod::lib", | ||
None, | ||
"you probably meant use::package instead", | ||
); | ||
} | ||
} | ||
} | ||
} |
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,12 @@ | ||
### What it does | ||
|
||
### Why is this bad? | ||
|
||
### Example | ||
``` | ||
// example code where clippy issues a warning | ||
``` | ||
Use instead: | ||
``` | ||
// example code which does not raise clippy warning | ||
``` |
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,3 @@ | ||
pub fn something() -> i32 { | ||
42 | ||
} |
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,9 @@ | ||
error[E0601]: `main` function not found in crate `lib` | ||
--> $DIR/lib.rs:3:2 | ||
| | ||
LL | } | ||
| ^ consider adding a `main` function to `$DIR/lib.rs` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0601`. |
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,8 @@ | ||
#![allow(unused)] | ||
#![warn(clippy::mod_lib)] | ||
|
||
mod lib; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a |
||
|
||
fn main() { | ||
println!("{}", lib::something()); | ||
} |
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,21 @@ | ||
error: found module declaration for lib.rs | ||
--> $DIR/main.rs:4:1 | ||
| | ||
LL | mod lib; | ||
| ^^^^^^^^ | ||
| | ||
= note: lib.rs is the root of this crate's library target | ||
= help: to refer to it from other targets, use the library's name as the path | ||
= note: `-D special-module-name` implied by `-D warnings` | ||
|
||
error: uncommon use of mod::lib | ||
--> $DIR/main.rs:4:1 | ||
| | ||
LL | mod lib; | ||
| ^^^^^^^^ | ||
| | ||
= help: you probably meant use::package instead | ||
= note: `-D clippy::mod-lib` implied by `-D warnings` | ||
|
||
error: aborting due to 2 previous errors | ||
|
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.
Hey, thanks for working on this! I know this PR isn’t ready for review, just wanted to say we’re trying to make all new lints to use ‘LateLintPass’, cheers! Feel free to share any of your issues
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.
Thanks @kraktus I've updated the code to implement
LateLintPass
instead. Am I going in the right direction here?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.
It does! Looks like your lint is not registered yet, using
cargo new_lint
(don’t remember the exact name of the subcommand, find it withcargo —help
while in clippy repo, will create the skeleton of the lint for youThere 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.
Actually
cargo dev update_lint
should suffice in the current situation. The lint exists, but it isn't yet registered.