Skip to content

Commit

Permalink
ptr_arg: honor allow attr on arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
ebroto committed May 25, 2020
1 parent c41916d commit a1824e1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
10 changes: 9 additions & 1 deletion clippy_lints/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::utils::ptr::get_spans;
use crate::utils::{
is_type_diagnostic_item, match_qpath, match_type, paths, snippet_opt, span_lint, span_lint_and_sugg,
is_allowed, is_type_diagnostic_item, match_qpath, match_type, paths, snippet_opt, span_lint, span_lint_and_sugg,
span_lint_and_then, walk_ptrs_hir_ty,
};
use if_chain::if_chain;
Expand Down Expand Up @@ -150,8 +150,16 @@ fn check_fn(cx: &LateContext<'_, '_>, decl: &FnDecl<'_>, fn_id: HirId, opt_body_
let fn_def_id = cx.tcx.hir().local_def_id(fn_id);
let sig = cx.tcx.fn_sig(fn_def_id);
let fn_ty = sig.skip_binder();
let body = opt_body_id.map(|id| cx.tcx.hir().body(id));

for (idx, (arg, ty)) in decl.inputs.iter().zip(fn_ty.inputs()).enumerate() {
// Honor the allow attribute on parameters. See issue 5644.
if let Some(body) = &body {
if is_allowed(cx, PTR_ARG, body.params[idx].hir_id) {
continue;
}
}

if let ty::Ref(_, ty, Mutability::Not) = ty.kind {
if is_type_diagnostic_item(cx, ty, sym!(vec_type)) {
let mut ty_snippet = None;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/utils/sugg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ pub trait DiagnosticBuilderExt<'a, T: LintContext> {

/// Suggest to add an item before another.
///
/// The item should not be indented (expect for inner indentation).
/// The item should not be indented (except for inner indentation).
///
/// # Example
///
Expand Down
32 changes: 31 additions & 1 deletion tests/ui/ptr_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ fn false_positive_capacity_too(x: &String) -> String {
#[allow(dead_code)]
fn test_cow_with_ref(c: &Cow<[i32]>) {}

#[allow(dead_code)]
fn test_cow(c: Cow<[i32]>) {
let _c = c;
}
Expand All @@ -84,3 +83,34 @@ trait Foo2 {
impl Foo2 for String {
fn do_string(&self) {}
}

// Check that the allow attribute on parameters is honored
mod issue_5644 {
use std::borrow::Cow;

fn allowed(
#[allow(clippy::ptr_arg)] _v: &Vec<u32>,
#[allow(clippy::ptr_arg)] _s: &String,
#[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
) {
}

struct S {}
impl S {
fn allowed(
#[allow(clippy::ptr_arg)] _v: &Vec<u32>,
#[allow(clippy::ptr_arg)] _s: &String,
#[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
) {
}
}

trait T {
fn allowed(
#[allow(clippy::ptr_arg)] _v: &Vec<u32>,
#[allow(clippy::ptr_arg)] _s: &String,
#[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
) {
}
}
}

0 comments on commit a1824e1

Please sign in to comment.