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

ptr_arg: honor allow attribute on arguments #5647

Merged
merged 1 commit into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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]>,
) {
}
}
}