diff options
| author | bors <bors@rust-lang.org> | 2019-03-05 10:12:53 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-03-05 10:12:53 +0000 |
| commit | a8f61e70a8c1c438658d0c30080fc3ce3be6c2df (patch) | |
| tree | 411427a73516f5d4fb63638d4b785edea82cbc1e | |
| parent | caccf8bd4c3d490d6a4cf329a3411bbf68753642 (diff) | |
| parent | 15cba2e9563bd736a6d18028b43482698775c23d (diff) | |
| download | rust-a8f61e70a8c1c438658d0c30080fc3ce3be6c2df.tar.gz rust-a8f61e70a8c1c438658d0c30080fc3ce3be6c2df.zip | |
Auto merge of #3844 - phansch:const_fn_external_macro, r=oli-obk
Fix two missing_const_for_fn false positives Fixes #3841 * Fixes false positive in external macros * Fixes false positive when implement trait methods
| -rw-r--r-- | clippy_lints/src/missing_const_for_fn.rs | 19 | ||||
| -rw-r--r-- | tests/ui/missing_const_for_fn/cant_be_const.rs | 13 |
2 files changed, 29 insertions, 3 deletions
diff --git a/clippy_lints/src/missing_const_for_fn.rs b/clippy_lints/src/missing_const_for_fn.rs index 00a3de0632f..633105ff60b 100644 --- a/clippy_lints/src/missing_const_for_fn.rs +++ b/clippy_lints/src/missing_const_for_fn.rs @@ -1,8 +1,9 @@ use crate::utils::{is_entrypoint_fn, span_lint}; +use if_chain::if_chain; use rustc::hir; use rustc::hir::intravisit::FnKind; use rustc::hir::{Body, Constness, FnDecl, HirId}; -use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; +use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; use rustc_mir::transform::qualify_min_const_fn::is_min_const_fn; use syntax_pos::Span; @@ -82,7 +83,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn { ) { let def_id = cx.tcx.hir().local_def_id_from_hir_id(hir_id); - if is_entrypoint_fn(cx, def_id) { + if in_external_macro(cx.tcx.sess, span) || is_entrypoint_fn(cx, def_id) { return; } @@ -95,7 +96,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn { } }, FnKind::Method(_, sig, ..) => { - if already_const(sig.header) { + if is_trait_method(cx, hir_id) || already_const(sig.header) { return; } }, @@ -114,6 +115,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn { } } +fn is_trait_method(cx: &LateContext<'_, '_>, hir_id: HirId) -> bool { + // Get the implemented trait for the current function + let parent_impl = cx.tcx.hir().get_parent_item(hir_id); + if_chain! { + if parent_impl != hir::CRATE_HIR_ID; + if let hir::Node::Item(item) = cx.tcx.hir().get_by_hir_id(parent_impl); + if let hir::ItemKind::Impl(_, _, _, _, Some(_trait_ref), _, _) = &item.node; + then { return true; } + } + false +} + // We don't have to lint on something that's already `const` fn already_const(header: hir::FnHeader) -> bool { header.constness == Constness::Const diff --git a/tests/ui/missing_const_for_fn/cant_be_const.rs b/tests/ui/missing_const_for_fn/cant_be_const.rs index 36efe16b84f..115cc954dc7 100644 --- a/tests/ui/missing_const_for_fn/cant_be_const.rs +++ b/tests/ui/missing_const_for_fn/cant_be_const.rs @@ -55,3 +55,16 @@ trait Foo { 33 } } + +// Don't lint in external macros (derive) +#[derive(PartialEq, Eq)] +struct Point(isize, isize); + +impl std::ops::Add for Point { + type Output = Self; + + // Don't lint in trait impls of derived methods + fn add(self, other: Self) -> Self { + Point(self.0 + other.0, self.1 + other.1) + } +} |
