Skip to content

Commit 2a6cfa7

Browse files
committed
Auto merge of rust-lang#5647 - ebroto:5644_allow_ptr_arg_in_arg_position, r=flip1995
ptr_arg: honor `allow` attribute on arguments The `intravisit::Visitor` impl for `LateContextAndPass` only takes into account the attributes of a function parameter inside the `check_param` method. `ptr_arg` starts its heuristics at `check_item` / `check_impl_item` / `check_trait_item`, so the `allow` is not taken into account automatically. changelog: ptr_arg: honor `allow` attribute on arguments Fixes rust-lang#5644
2 parents dd06d29 + a1824e1 commit 2a6cfa7

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

clippy_lints/src/ptr.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::utils::ptr::get_spans;
44
use crate::utils::{
5-
is_type_diagnostic_item, match_qpath, match_type, paths, snippet_opt, span_lint, span_lint_and_sugg,
5+
is_allowed, is_type_diagnostic_item, match_qpath, match_type, paths, snippet_opt, span_lint, span_lint_and_sugg,
66
span_lint_and_then, walk_ptrs_hir_ty,
77
};
88
use if_chain::if_chain;
@@ -150,8 +150,16 @@ fn check_fn(cx: &LateContext<'_, '_>, decl: &FnDecl<'_>, fn_id: HirId, opt_body_
150150
let fn_def_id = cx.tcx.hir().local_def_id(fn_id);
151151
let sig = cx.tcx.fn_sig(fn_def_id);
152152
let fn_ty = sig.skip_binder();
153+
let body = opt_body_id.map(|id| cx.tcx.hir().body(id));
153154

154155
for (idx, (arg, ty)) in decl.inputs.iter().zip(fn_ty.inputs()).enumerate() {
156+
// Honor the allow attribute on parameters. See issue 5644.
157+
if let Some(body) = &body {
158+
if is_allowed(cx, PTR_ARG, body.params[idx].hir_id) {
159+
continue;
160+
}
161+
}
162+
155163
if let ty::Ref(_, ty, Mutability::Not) = ty.kind {
156164
if is_type_diagnostic_item(cx, ty, sym!(vec_type)) {
157165
let mut ty_snippet = None;

clippy_lints/src/utils/sugg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ pub trait DiagnosticBuilderExt<'a, T: LintContext> {
530530

531531
/// Suggest to add an item before another.
532532
///
533-
/// The item should not be indented (expect for inner indentation).
533+
/// The item should not be indented (except for inner indentation).
534534
///
535535
/// # Example
536536
///

tests/ui/ptr_arg.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ fn false_positive_capacity_too(x: &String) -> String {
7171
#[allow(dead_code)]
7272
fn test_cow_with_ref(c: &Cow<[i32]>) {}
7373

74-
#[allow(dead_code)]
7574
fn test_cow(c: Cow<[i32]>) {
7675
let _c = c;
7776
}
@@ -84,3 +83,34 @@ trait Foo2 {
8483
impl Foo2 for String {
8584
fn do_string(&self) {}
8685
}
86+
87+
// Check that the allow attribute on parameters is honored
88+
mod issue_5644 {
89+
use std::borrow::Cow;
90+
91+
fn allowed(
92+
#[allow(clippy::ptr_arg)] _v: &Vec<u32>,
93+
#[allow(clippy::ptr_arg)] _s: &String,
94+
#[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
95+
) {
96+
}
97+
98+
struct S {}
99+
impl S {
100+
fn allowed(
101+
#[allow(clippy::ptr_arg)] _v: &Vec<u32>,
102+
#[allow(clippy::ptr_arg)] _s: &String,
103+
#[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
104+
) {
105+
}
106+
}
107+
108+
trait T {
109+
fn allowed(
110+
#[allow(clippy::ptr_arg)] _v: &Vec<u32>,
111+
#[allow(clippy::ptr_arg)] _s: &String,
112+
#[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
113+
) {
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)