Skip to content
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

Rollup of 13 pull requests #101289

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
4dded23
add `special_module_name` lint
ibraheemdev Mar 1, 2022
c08f460
tidy
ibraheemdev Apr 15, 2022
f479289
move dummy test module to auxiliary directory
ibraheemdev Jun 4, 2022
4fdf43f
`special_module_name`: ignore inline modules
ibraheemdev Jul 21, 2022
b9c47f6
Use getuid to check instead of USER env var in rustbuild
Samyak2 Aug 21, 2022
e136e02
bootstrap: Add llvm-has-rust-patches target option
tmandry Aug 27, 2022
73958fd
Ignore cargo target folder in src/bootstrap
tmandry Aug 27, 2022
de6a3ec
Make docs formulation more consistent for NonZero{int}
yjhn Aug 30, 2022
8410df3
Also replace the version placeholder in rustc_attr
est31 Aug 30, 2022
803e35a
Remove unneeded extra whitespace before where clause
GuillaumeGomez Aug 31, 2022
4304d1d
Update rustdoc tests
GuillaumeGomez Aug 31, 2022
b112bfe
Add rustdoc GUI test
GuillaumeGomez Aug 31, 2022
a928255
Fix bad target name in Walkthrough
diminishedprime Aug 31, 2022
037a911
rustdoc: remove unused `.docblock .impl-items` CSS
notriddle Aug 31, 2022
d8b572b
Tweaks to fuchsia doc walkthrough
andrewpollack Aug 31, 2022
a20318d
Update outdated comment about output capturing in print_to.
m-ou-se Sep 1, 2022
098725c
Fix filename of armv4t-none-eabi.md
QuinnPainter Sep 1, 2022
c9f4af6
Fix typo in comment
aDotInTheVoid Sep 1, 2022
78e9bea
do not suggest adding `move` to closure when `move` is already used
TaKO8Ki Sep 1, 2022
96ae006
Rollup merge of #94467 - ibraheemdev:master, r=pnkfelix
matthiaskrgr Sep 1, 2022
08e8695
Rollup merge of #100852 - Samyak2:samyak/100459, r=Mark-Simulacrum
matthiaskrgr Sep 1, 2022
827d33f
Rollup merge of #101072 - tmandry:llvm-is-vanilla, r=Mark-Simulacrum
matthiaskrgr Sep 1, 2022
f40cb90
Rollup merge of #101190 - yjhn:patch-1, r=Mark-Simulacrum
matthiaskrgr Sep 1, 2022
6a4f64c
Rollup merge of #101215 - est31:rustdoc_version_placeholder, r=Mark-S…
matthiaskrgr Sep 1, 2022
a60dc0c
Rollup merge of #101245 - GuillaumeGomez:remove-unneeded-where-whites…
matthiaskrgr Sep 1, 2022
3a68093
Rollup merge of #101251 - diminishedprime:patch-1, r=JohnTitor
matthiaskrgr Sep 1, 2022
a435d06
Rollup merge of #101254 - rust-lang:notriddle/remove-even-more-css, r…
matthiaskrgr Sep 1, 2022
1cb8ff0
Rollup merge of #101256 - andrewpollack:fuchsia-docs-adding, r=tmandry
matthiaskrgr Sep 1, 2022
5453c8d
Rollup merge of #101270 - m-ou-se:update-comment, r=joshtriplett
matthiaskrgr Sep 1, 2022
a25eef1
Rollup merge of #101271 - QuinnPainter:patch-1, r=Dylan-DPC
matthiaskrgr Sep 1, 2022
a3d1970
Rollup merge of #101274 - aDotInTheVoid:comment-typo, r=TaKO8Ki
matthiaskrgr Sep 1, 2022
22a30bd
Rollup merge of #101285 - TaKO8Ki:do-not-suggest-adding-move-when-clo…
matthiaskrgr Sep 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add special_module_name lint
  • Loading branch information
ibraheemdev committed Mar 1, 2022
commit 4dded23925994103e3e793d5edbe0d30222cdaa4
74 changes: 74 additions & 0 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3248,3 +3248,77 @@ impl<'tcx> LateLintPass<'tcx> for NamedAsmLabels {
}
}
}

declare_lint! {
/// The `special_module_name` lint detects module
/// declarations for files that have a special meaning.
///
/// ### Example
///
/// ```rust,compile_fail
/// mod lib;
///
/// fn main() {
/// lib::run();
/// }
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// Cargo recognizes `lib.rs` and `main.rs` as the root of a
/// library or binary crate, so declaring them as modules
/// will lead to miscompilation of the crate unless configured
/// explicitly.
///
/// To access a library from a binary target within the same crate,
/// use `your_crate_name::` as the path path instead of `lib::`:
///
/// ```rust,compile_fail
/// // bar/src/lib.rs
/// fn run() {
/// // ...
/// }
///
/// // bar/src/main.rs
/// fn main() {
/// bar::run();
/// }
/// ```
///
/// Binary targets cannot be used as libraries and so declaring
/// one as a module is not allowed.
pub SPECIAL_MODULE_NAME,
Warn,
"module declarations for files with a special meaning",
}

declare_lint_pass!(SpecialModuleName => [SPECIAL_MODULE_NAME]);

impl EarlyLintPass for SpecialModuleName {
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &ast::Crate) {
for item in &krate.items {
if let ast::ItemKind::Mod(..) = item.kind {
if item.attrs.iter().any(|a| a.has_name(sym::path)) {
continue;
}

match item.ident.name.as_str() {
"lib" => cx.struct_span_lint(SPECIAL_MODULE_NAME, item.span, |lint| {
lint.build("found module declaration for lib.rs")
.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")
.emit()
}),
"main" => cx.struct_span_lint(SPECIAL_MODULE_NAME, item.span, |lint| {
lint.build("found module declaration for main.rs")
.note("a binary crate cannot be used as library")
.emit()
}),
_ => continue
}
}
}
}
}
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ macro_rules! early_lint_passes {
UnusedBraces: UnusedBraces,
UnusedImportBraces: UnusedImportBraces,
UnsafeCode: UnsafeCode,
SpecialModuleName: SpecialModuleName,
AnonymousParameters: AnonymousParameters,
EllipsisInclusiveRangePatterns: EllipsisInclusiveRangePatterns::default(),
NonCamelCaseTypes: NonCamelCaseTypes,
Expand Down
Empty file added src/test/ui/modules/dummy.rs
Empty file.
8 changes: 8 additions & 0 deletions src/test/ui/modules/special_module_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod lib;
//~^ WARN found module declaration for lib.rs
//~| ERROR file not found for module `lib`
mod main;
//~^ WARN found module declaration for main.rs
//~| ERROR file not found for module `main`

fn main() {}
37 changes: 37 additions & 0 deletions src/test/ui/modules/special_module_name.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
error[E0583]: file not found for module `lib`
--> $DIR/special_module_name.rs:1:1
|
LL | mod lib;
| ^^^^^^^^
|
= help: to create the module `lib`, create file "$DIR/lib.rs" or "$DIR/lib/mod.rs"

error[E0583]: file not found for module `main`
--> $DIR/special_module_name.rs:4:1
|
LL | mod main;
| ^^^^^^^^^
|
= help: to create the module `main`, create file "$DIR/main.rs" or "$DIR/main/mod.rs"

warning: found module declaration for lib.rs
--> $DIR/special_module_name.rs:1:1
|
LL | mod lib;
| ^^^^^^^^
|
= note: `#[warn(special_module_name)]` on by default
= 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

warning: found module declaration for main.rs
--> $DIR/special_module_name.rs:4:1
|
LL | mod main;
| ^^^^^^^^^
|
= note: a binary crate cannot be used as library

error: aborting due to 2 previous errors; 2 warnings emitted

For more information about this error, try `rustc --explain E0583`.
9 changes: 9 additions & 0 deletions src/test/ui/modules/special_module_name_ignore.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// run-pass

#[path = "dummy.rs"]
mod lib;

#[path = "dummy.rs"]
mod main;

fn main() {}