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

Clippy subtree sync #116489

Merged
merged 83 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
1c77b0b
Move needless_raw_string_hashes to pedantic
Alexendoo Aug 27, 2023
b5941a2
Partially outline code inside the panic! macro
Zoxc Sep 3, 2023
39f7f69
Add msrv test template for `cargo dev new_lint --msrv`
Alexendoo Sep 23, 2023
772296c
Merge commit '7671c283a50b5d1168841f3014b14000f01dd204' into clippyup
flip1995 Sep 25, 2023
6ad218c
Add colored help
blyxyas Sep 13, 2023
78ddc8d
Auto merge of #11495 - blyxyas:help_message_reformat, r=flip1995
bors Sep 25, 2023
1972cc8
Test that each config value exists in a test clippy.toml
Alexendoo Sep 25, 2023
84d6894
it's not Miri but MIR constants
RalfJung Sep 25, 2023
ce45221
simply some valtree-to-const conversion
RalfJung Sep 25, 2023
0f19857
mir_to_const: fix handling of float arrays
RalfJung Sep 25, 2023
6c48ef3
Auto merge of #11557 - Alexendoo:dev-new-lint-msrv-test, r=Manishearth
bors Sep 25, 2023
bf4c998
Auto merge of #11564 - Alexendoo:config-test-test, r=giraffate
bors Sep 26, 2023
5507482
Don't store lazyness in DefKind
compiler-errors Sep 26, 2023
fab9000
Do not lint when imported item contains underscore
koka831 Feb 6, 2023
f2efdba
subst -> instantiate
lcnr Sep 25, 2023
585b56f
Auto merge of #10300 - koka831:fix/9942, r=xFrednet
bors Sep 26, 2023
6cdff10
Describe the type of string in raw_strings lints
Alexendoo Sep 26, 2023
ec2f626
Add `manual_hash_one` lint
Alexendoo Sep 23, 2023
493ab53
Auto merge of #11556 - Alexendoo:manual-hash-one, r=Manishearth
bors Sep 26, 2023
4494b69
Auto merge of #11569 - Alexendoo:needless-raw-string-descr, r=llogiq
bors Sep 26, 2023
7f132e8
Auto merge of #116144 - lcnr:subst-less, r=oli-obk
bors Sep 26, 2023
3b75db7
Auto merge of #116163 - compiler-errors:lazyness, r=oli-obk
bors Sep 27, 2023
62b8ef3
Mention that `missing_assert_message` lint ignores test functions
unexge Sep 27, 2023
124f1b0
Auto merge of #11574 - unexge:missing-assert-message-docs, r=Alexendoo
bors Sep 28, 2023
b413bf6
Fix index of the remaining positional arguments
koka831 Sep 28, 2023
29ed6fa
Auto merge of #11415 - Alexendoo:needless-raw-string-hashes-pedantic,…
bors Sep 28, 2023
d18d01a
Auto merge of #11576 - koka831:fix/10128, r=llogiq
bors Sep 28, 2023
91997a4
Auto merge of #11565 - RalfJung:mir_to_const, r=Jarcho
bors Sep 28, 2023
82207f4
Move needless_raw_string_hashes to pedantic
Alexendoo Aug 27, 2023
330ebbb
new lint: `iter_without_into_iter`
y21 Sep 17, 2023
38fd80a
Remove `rustc_lint_defs::lint_array`
DaniPopes Sep 28, 2023
a4d11d9
Reverse postorder instead of using reversed postorder
WaffleLapkin Sep 28, 2023
ad5653b
Add missing lint description headers
DaniPopes Sep 29, 2023
d38fa1a
Auto merge of #11527 - y21:iter_without_into_iter, r=Jarcho
bors Sep 29, 2023
1a82ca0
Rollup merge of #116245 - flip1995:clippy-backport, r=Manishearth
matthiaskrgr Sep 29, 2023
67a83ff
Auto merge of #11582 - DaniPopes:missing-headers, r=xFrednet
bors Sep 29, 2023
2d20179
[`manual_let_else`]: only omit block if span is from same ctxt
y21 Sep 28, 2023
b00236d
Auto merge of #11580 - y21:issue11579, r=Jarcho
bors Sep 29, 2023
ad0b7ed
Auto merge of #116254 - WaffleLapkin:nicen-traversal, r=cjgillot
bors Sep 30, 2023
44b6aca
Avoid linting in external/proc macro
koka831 Sep 30, 2023
d5cc97e
Add macro for test which use std internally
koka831 Sep 30, 2023
8eb586d
new lint: `into_iter_without_iter`
y21 Sep 30, 2023
0e43a04
Auto merge of #11587 - y21:into_iter_without_iter, r=Jarcho
bors Sep 30, 2023
e683e3e
Don't lint `manual_non_exhaustive` when enum explicitly marked as `no…
Tyrubias Oct 1, 2023
3169423
Auto merge of #115670 - Zoxc:outline-panic-macro-1, r=Mark-Simulacrum
bors Oct 1, 2023
9dfd60c
Remove extraneous `#[non_exhaustive]` check in lint
Tyrubias Oct 1, 2023
6f1a78f
Use Span#from_expansion instead of in_external_macro
koka831 Oct 1, 2023
0f8b862
Fix documentation link
schubart Oct 1, 2023
cbe67bc
Auto merge of #11590 - Tyrubias:non_ex_false_positive, r=Alexendoo
bors Oct 1, 2023
ec15630
Auto merge of #11592 - schubart:fix_documentation_link, r=flip1995
bors Oct 1, 2023
aee3daf
Auto merge of #11593 - koka831:fix/10511, r=xFrednet
bors Oct 1, 2023
258b9a8
Don't escape unicode escape braces in print_literal
Alexendoo Jul 31, 2023
331d01e
Auto merge of #11265 - Alexendoo:print-literal-unicode-escapes, r=llogiq
bors Oct 1, 2023
3f0da4d
Move `needless_pass_by_ref_mut`: `suspicious` -> `nursery`
blyxyas Oct 2, 2023
07e6329
Modify tests to account for the new allow-by-default `needless_pass_b…
blyxyas Oct 2, 2023
08c429f
Auto merge of #11596 - blyxyas:fix-fp-needless_pass_by_ref_mut, r=Jarcho
bors Oct 2, 2023
ea4b388
subtyping_projections
ouz-a Aug 16, 2023
81400e2
Auto merge of #11589 - koka831:fix/10198, r=giraffate
bors Oct 3, 2023
2c525fd
Point to full async fn for future
compiler-errors Oct 2, 2023
f107a50
Auto merge of #115025 - ouz-a:ouz_testing, r=lcnr
bors Oct 3, 2023
e465264
Avoid invoking `ignored_unit_patterns` in macro definition
koka831 Oct 3, 2023
1a56f90
Fix: avoid changing drop order
koka831 Oct 3, 2023
29958f0
Auto merge of #11602 - koka831:fix/11601, r=xFrednet
bors Oct 3, 2023
c715267
Apply review suggestions from @y21
koka831 Oct 3, 2023
b437069
Auto merge of #11603 - koka831:fix/11599, r=y21
bors Oct 3, 2023
eab0a75
Update version attribute for 1.73 lints
xFrednet Oct 3, 2023
8d920a8
Changelog for Rust 1.73 :pen:
xFrednet Oct 3, 2023
9483555
sudo CI=green
xFrednet Oct 3, 2023
404217e
Address PR Review <3
xFrednet Oct 4, 2023
716f211
Auto merge of #116360 - compiler-errors:async-span, r=oli-obk
bors Oct 4, 2023
9554e47
Auto merge of #11605 - xFrednet:changelog-1-73, r=blyxyas
bors Oct 4, 2023
56794fa
Fix clippy
compiler-errors Oct 2, 2023
c5c6d70
Point to closure return instead of output if defaulted
compiler-errors Oct 2, 2023
dcc4001
Fix `items_after_test_module` for non root modules, add applicable su…
Alexendoo Oct 4, 2023
48d2770
Improve `redundant_locals` help message
koka831 Oct 6, 2023
279127c
Auto merge of #11611 - Alexendoo:items-after-test-module-check-crate,…
bors Oct 6, 2023
7217c0f
Auto merge of #11628 - koka831:fix/11625, r=blyxyas
bors Oct 6, 2023
82c3064
Merge remote-tracking branch 'upstream/master' into rustup
flip1995 Oct 6, 2023
50754da
Bump nightly version -> 2023-10-06
flip1995 Oct 6, 2023
b8677e5
Bump Clippy version -> 0.1.75
flip1995 Oct 6, 2023
b105fb4
Auto merge of #11629 - flip1995:rustup, r=flip1995
bors Oct 6, 2023
3c8c3ad
Merge commit 'b105fb4c39bc1a010807a6c076193cef8d93c109' into clippyup
flip1995 Oct 6, 2023
6233d44
Update Cargo.lock
flip1995 Oct 6, 2023
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
Prev Previous commit
Next Next commit
Fix items_after_test_module for non root modules, add applicable su…
…ggestion
  • Loading branch information
Alexendoo committed Oct 6, 2023
commit dcc400191e444c8a57869f5004fdcbab807f7f09
108 changes: 68 additions & 40 deletions clippy_lints/src/items_after_test_module.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::{is_from_proc_macro, is_in_cfg_test};
use rustc_hir::{HirId, ItemId, ItemKind, Mod};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::source::snippet_opt;
use clippy_utils::{fulfill_or_allowed, is_cfg_test, is_from_proc_macro};
use rustc_errors::{Applicability, SuggestionStyle};
use rustc_hir::{HirId, Item, ItemKind, Mod};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::{sym, Span};
use rustc_span::hygiene::AstPass;
use rustc_span::{sym, ExpnKind};

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -41,46 +43,72 @@ declare_clippy_lint! {

declare_lint_pass!(ItemsAfterTestModule => [ITEMS_AFTER_TEST_MODULE]);

impl LateLintPass<'_> for ItemsAfterTestModule {
fn check_mod(&mut self, cx: &LateContext<'_>, _: &Mod<'_>, _: HirId) {
let mut was_test_mod_visited = false;
let mut test_mod_span: Option<Span> = None;
fn cfg_test_module<'tcx>(cx: &LateContext<'tcx>, item: &Item<'tcx>) -> bool {
if let ItemKind::Mod(test_mod) = item.kind
&& item.span.hi() == test_mod.spans.inner_span.hi()
&& is_cfg_test(cx.tcx, item.hir_id())
&& !item.span.from_expansion()
&& !is_from_proc_macro(cx, item)
{
true
} else {
false
}
}

let hir = cx.tcx.hir();
let items = hir.items().collect::<Vec<ItemId>>();
impl LateLintPass<'_> for ItemsAfterTestModule {
fn check_mod(&mut self, cx: &LateContext<'_>, module: &Mod<'_>, _: HirId) {
let mut items = module.item_ids.iter().map(|&id| cx.tcx.hir().item(id));

for (i, itid) in items.iter().enumerate() {
let item = hir.item(*itid);
let Some((mod_pos, test_mod)) = items.by_ref().enumerate().find(|(_, item)| cfg_test_module(cx, item)) else {
return;
};

if_chain! {
if was_test_mod_visited;
if i == (items.len() - 3 /* Weird magic number (HIR-translation behaviour) */);
if cx.sess().source_map().lookup_char_pos(item.span.lo()).file.name_hash
== cx.sess().source_map().lookup_char_pos(test_mod_span.unwrap().lo()).file.name_hash; // Will never fail
if !matches!(item.kind, ItemKind::Mod(_));
if !is_in_cfg_test(cx.tcx, itid.hir_id()); // The item isn't in the testing module itself
if !in_external_macro(cx.sess(), item.span);
if !is_from_proc_macro(cx, item);
let after: Vec<_> = items
.filter(|item| {
// Ignore the generated test main function
!(item.ident.name == sym::main
&& item.span.ctxt().outer_expn_data().kind == ExpnKind::AstPass(AstPass::TestHarness))
})
.collect();

then {
span_lint_and_help(cx, ITEMS_AFTER_TEST_MODULE, test_mod_span.unwrap().with_hi(item.span.hi()), "items were found after the testing module", None, "move the items to before the testing module was defined");
}};
if let Some(last) = after.last()
&& after.iter().all(|&item| {
!matches!(item.kind, ItemKind::Mod(_))
&& !item.span.from_expansion()
&& !is_from_proc_macro(cx, item)
})
&& !fulfill_or_allowed(cx, ITEMS_AFTER_TEST_MODULE, after.iter().map(|item| item.hir_id()))
{
let def_spans: Vec<_> = std::iter::once(test_mod.owner_id)
.chain(after.iter().map(|item| item.owner_id))
.map(|id| cx.tcx.def_span(id))
.collect();

if let ItemKind::Mod(module) = item.kind && item.span.hi() == module.spans.inner_span.hi() {
// Check that it works the same way, the only I way I've found for #10713
for attr in cx.tcx.get_attrs(item.owner_id.to_def_id(), sym::cfg) {
if_chain! {
if attr.has_name(sym::cfg);
if let Some(mitems) = attr.meta_item_list();
if let [mitem] = &*mitems;
if mitem.has_name(sym::test);
then {
was_test_mod_visited = true;
test_mod_span = Some(item.span);
}
span_lint_hir_and_then(
cx,
ITEMS_AFTER_TEST_MODULE,
test_mod.hir_id(),
def_spans,
"items after a test module",
|diag| {
if let Some(prev) = mod_pos.checked_sub(1)
&& let prev = cx.tcx.hir().item(module.item_ids[prev])
&& let items_span = last.span.with_lo(test_mod.span.hi())
&& let Some(items) = snippet_opt(cx, items_span)
{
diag.multipart_suggestion_with_style(
"move the items to before the test module was defined",
vec![
(prev.span.shrink_to_hi(), items),
(items_span, String::new())
],
Applicability::MachineApplicable,
SuggestionStyle::HideCodeAlways,
);
}
}
}
},
);
}
}
}
21 changes: 12 additions & 9 deletions clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ use std::sync::{Mutex, MutexGuard, OnceLock};
use if_chain::if_chain;
use itertools::Itertools;
use rustc_ast::ast::{self, LitKind, RangeLimits};
use rustc_ast::Attribute;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::unhash::UnhashMap;
use rustc_hir::def::{DefKind, Res};
Expand Down Expand Up @@ -2452,11 +2451,12 @@ pub fn is_in_test_function(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
})
}

/// Checks if the item containing the given `HirId` has `#[cfg(test)]` attribute applied
/// Checks if `id` has a `#[cfg(test)]` attribute applied
///
/// Note: Add `//@compile-flags: --test` to UI tests with a `#[cfg(test)]` function
pub fn is_in_cfg_test(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
fn is_cfg_test(attr: &Attribute) -> bool {
/// This only checks directly applied attributes, to see if a node is inside a `#[cfg(test)]` parent
/// use [`is_in_cfg_test`]
pub fn is_cfg_test(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
tcx.hir().attrs(id).iter().any(|attr| {
if attr.has_name(sym::cfg)
&& let Some(items) = attr.meta_item_list()
&& let [item] = &*items
Expand All @@ -2466,11 +2466,14 @@ pub fn is_in_cfg_test(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
} else {
false
}
}
})
}

/// Checks if any parent node of `HirId` has `#[cfg(test)]` attribute applied
pub fn is_in_cfg_test(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
tcx.hir()
.parent_iter(id)
.flat_map(|(parent_id, _)| tcx.hir().attrs(parent_id))
.any(is_cfg_test)
.parent_id_iter(id)
.any(|parent_id| is_cfg_test(tcx, parent_id))
}

/// Checks if the item of any of its parents has `#[cfg(...)]` attribute applied.
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/items_after_test_module/after_proc_macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@aux-build:../auxiliary/proc_macros.rs
extern crate proc_macros;

proc_macros::with_span! {
span
#[cfg(test)]
mod tests {}
}

#[test]
fn f() {}
4 changes: 4 additions & 0 deletions tests/ui/items_after_test_module/auxiliary/submodule.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[cfg(test)]
mod tests {}

fn in_submodule() {}
2 changes: 0 additions & 2 deletions tests/ui/items_after_test_module/block_module.stderr

This file was deleted.

8 changes: 8 additions & 0 deletions tests/ui/items_after_test_module/in_submodule.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[path = "auxiliary/submodule.rs"]
mod submodule;

#[cfg(test)]
mod tests {
#[test]
fn t() {}
}
14 changes: 14 additions & 0 deletions tests/ui/items_after_test_module/in_submodule.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: items after a test module
--> $DIR/auxiliary/submodule.rs:2:1
|
LL | mod tests {}
| ^^^^^^^^^
LL |
LL | fn in_submodule() {}
| ^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::items-after-test-module` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::items_after_test_module)]`

error: aborting due to previous error

11 changes: 11 additions & 0 deletions tests/ui/items_after_test_module/multiple_modules.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[cfg(test)]
mod tests {
#[test]
fn f() {}
}

#[cfg(test)]
mod more_tests {
#[test]
fn g() {}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
//@compile-flags: --test
#![allow(unused)]
#![warn(clippy::items_after_test_module)]

fn main() {}

fn should_not_lint() {}

fn should_lint() {}

const SHOULD_ALSO_LINT: usize = 1;
macro_rules! should_lint {
() => {};
}

#[allow(dead_code)]
#[allow(unused)] // Some attributes to check that span replacement is good enough
#[allow(clippy::allow_attributes)]
Expand All @@ -14,10 +20,3 @@ mod tests {
#[test]
fn hi() {}
}

fn should_lint() {}

const SHOULD_ALSO_LINT: usize = 1;
macro_rules! should_not_lint {
() => {};
}
22 changes: 22 additions & 0 deletions tests/ui/items_after_test_module/root_module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![allow(unused)]
#![warn(clippy::items_after_test_module)]

fn main() {}

fn should_not_lint() {}

#[allow(dead_code)]
#[allow(unused)] // Some attributes to check that span replacement is good enough
#[allow(clippy::allow_attributes)]
#[cfg(test)]
mod tests {
#[test]
fn hi() {}
}

fn should_lint() {}

const SHOULD_ALSO_LINT: usize = 1;
macro_rules! should_lint {
() => {};
}
20 changes: 20 additions & 0 deletions tests/ui/items_after_test_module/root_module.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: items after a test module
--> $DIR/root_module.rs:12:1
|
LL | mod tests {
| ^^^^^^^^^
...
LL | fn should_lint() {}
| ^^^^^^^^^^^^^^^^
LL |
LL | const SHOULD_ALSO_LINT: usize = 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | macro_rules! should_lint {
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::items-after-test-module` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::items_after_test_module)]`
= help: move the items to before the test module was defined

error: aborting due to previous error