From cadac5b6a8f13ace36049febf8cde3a629d3d30c Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Mon, 21 Jan 2019 07:54:05 +0100 Subject: [PATCH] cargo fmt --- clippy_lints/src/missing_const_for_fn.rs | 36 ++++++++++--------- clippy_lints/src/utils/mod.rs | 8 ++--- .../ui/missing_const_for_fn/cant_be_const.rs | 18 +++++++--- .../ui/missing_const_for_fn/could_be_const.rs | 12 ++++--- .../could_be_const.stderr | 20 ++++++----- 5 files changed, 54 insertions(+), 40 deletions(-) diff --git a/clippy_lints/src/missing_const_for_fn.rs b/clippy_lints/src/missing_const_for_fn.rs index 151c82133ec1..7d24b6a55090 100644 --- a/clippy_lints/src/missing_const_for_fn.rs +++ b/clippy_lints/src/missing_const_for_fn.rs @@ -1,13 +1,13 @@ use rustc::hir; -use rustc::hir::{Body, FnDecl, Constness}; use rustc::hir::intravisit::FnKind; +use rustc::hir::{Body, Constness, FnDecl}; // use rustc::mir::*; -use syntax::ast::{NodeId, Attribute}; -use syntax_pos::Span; +use crate::utils::{is_entrypoint_fn, span_lint}; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_tool_lint, lint_array}; use rustc_mir::transform::qualify_min_const_fn::is_min_const_fn; -use crate::utils::{span_lint, is_entrypoint_fn}; +use syntax::ast::{Attribute, NodeId}; +use syntax_pos::Span; /// **What it does:** /// @@ -26,8 +26,12 @@ use crate::utils::{span_lint, is_entrypoint_fn}; /// Also, the lint only runs one pass over the code. Consider these two non-const functions: /// /// ```rust -/// fn a() -> i32 { 0 } -/// fn b() -> i32 { a() } +/// fn a() -> i32 { +/// 0 +/// } +/// fn b() -> i32 { +/// a() +/// } /// ``` /// /// When running Clippy, the lint will only suggest to make `a` const, because `b` at this time @@ -38,9 +42,7 @@ use crate::utils::{span_lint, is_entrypoint_fn}; /// /// ```rust /// fn new() -> Self { -/// Self { -/// random_number: 42 -/// } +/// Self { random_number: 42 } /// } /// ``` /// @@ -48,9 +50,7 @@ use crate::utils::{span_lint, is_entrypoint_fn}; /// /// ```rust /// const fn new() -> Self { -/// Self { -/// random_number: 42 -/// } +/// Self { random_number: 42 } /// } /// ``` declare_clippy_lint! { @@ -76,7 +76,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn { _: &FnDecl, _: &Body, span: Span, - node_id: NodeId + node_id: NodeId, ) { // Perform some preliminary checks that rule out constness on the Clippy side. This way we // can skip the actual const check and return early. @@ -93,7 +93,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn { return; } }, - _ => return + _ => return, } let def_id = cx.tcx.hir().local_def_id(node_id); @@ -111,9 +111,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingConstForFn { fn can_be_const_fn(name: &str, header: hir::FnHeader, attrs: &[Attribute]) -> bool { // Main and custom entrypoints can't be `const` - if is_entrypoint_fn(name, attrs) { return false } + if is_entrypoint_fn(name, attrs) { + return false; + } // We don't have to lint on something that's already `const` - if header.constness == Constness::Const { return false } + if header.constness == Constness::Const { + return false; + } true } diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index ee6de4956686..18d15c4debd6 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -334,11 +334,9 @@ pub fn method_chain_args<'a>(expr: &'a Expr, methods: &[&str]) -> Option bool { - - let is_custom_entrypoint = attrs.iter().any(|attr| { - attr.path.segments.len() == 1 - && attr.path.segments[0].ident.to_string() == "start" - }); + let is_custom_entrypoint = attrs + .iter() + .any(|attr| attr.path.segments.len() == 1 && attr.path.segments[0].ident.to_string() == "start"); is_custom_entrypoint || fn_name == "main" } 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 cfaf01cf3c11..ede3724cc6b0 100644 --- a/tests/ui/missing_const_for_fn/cant_be_const.rs +++ b/tests/ui/missing_const_for_fn/cant_be_const.rs @@ -8,16 +8,22 @@ struct Game; // This should not be linted because it's already const -const fn already_const() -> i32 { 32 } +const fn already_const() -> i32 { + 32 +} impl Game { // This should not be linted because it's already const - pub const fn already_const() -> i32 { 32 } + pub const fn already_const() -> i32 { + 32 + } } // Allowing on this function, because it would lint, which we don't want in this case. #[allow(clippy::missing_const_for_fn)] -fn random() -> u32 { 42 } +fn random() -> u32 { + 42 +} // We should not suggest to make this function `const` because `random()` is non-const fn random_caller() -> u32 { @@ -30,7 +36,7 @@ static Y: u32 = 0; // refer to a static variable fn get_y() -> u32 { Y - //~^ ERROR E0013 + //~^ ERROR E0013 } // Also main should not be suggested to be made const @@ -52,4 +58,6 @@ trait Foo { // Don't lint custom entrypoints either #[start] -fn init(num: isize, something: *const *const u8) -> isize { 1 } +fn init(num: isize, something: *const *const u8) -> isize { + 1 +} diff --git a/tests/ui/missing_const_for_fn/could_be_const.rs b/tests/ui/missing_const_for_fn/could_be_const.rs index 3ba39711e8d6..2c0a8e7a3c11 100644 --- a/tests/ui/missing_const_for_fn/could_be_const.rs +++ b/tests/ui/missing_const_for_fn/could_be_const.rs @@ -10,14 +10,14 @@ struct Game { impl Game { // Could be const pub fn new() -> Self { - Self { - guess: 42, - } + Self { guess: 42 } } } // Could be const -fn one() -> i32 { 1 } +fn one() -> i32 { + 1 +} // Could also be const fn two() -> i32 { @@ -32,7 +32,9 @@ fn string() -> String { } // Could be const -unsafe fn four() -> i32 { 4 } +unsafe fn four() -> i32 { + 4 +} // Could also be const fn generic(t: T) -> T { diff --git a/tests/ui/missing_const_for_fn/could_be_const.stderr b/tests/ui/missing_const_for_fn/could_be_const.stderr index 593f9cf810ac..22ea852905da 100644 --- a/tests/ui/missing_const_for_fn/could_be_const.stderr +++ b/tests/ui/missing_const_for_fn/could_be_const.stderr @@ -2,19 +2,19 @@ error: this could be a const_fn --> $DIR/could_be_const.rs:12:5 | LL | / pub fn new() -> Self { -LL | | Self { -LL | | guess: 42, -LL | | } +LL | | Self { guess: 42 } LL | | } | |_____^ | = note: `-D clippy::missing-const-for-fn` implied by `-D warnings` error: this could be a const_fn - --> $DIR/could_be_const.rs:20:1 + --> $DIR/could_be_const.rs:18:1 | -LL | fn one() -> i32 { 1 } - | ^^^^^^^^^^^^^^^^^^^^^ +LL | / fn one() -> i32 { +LL | | 1 +LL | | } + | |_^ error: this could be a const_fn --> $DIR/could_be_const.rs:23:1 @@ -36,11 +36,13 @@ LL | | } error: this could be a const_fn --> $DIR/could_be_const.rs:35:1 | -LL | unsafe fn four() -> i32 { 4 } - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | / unsafe fn four() -> i32 { +LL | | 4 +LL | | } + | |_^ error: this could be a const_fn - --> $DIR/could_be_const.rs:38:1 + --> $DIR/could_be_const.rs:40:1 | LL | / fn generic(t: T) -> T { LL | | t