]> git.lizzy.rs Git - rust.git/commitdiff
cargo fmt
authorPhilipp Hansch <dev@phansch.net>
Mon, 21 Jan 2019 06:54:05 +0000 (07:54 +0100)
committerPhilipp Hansch <dev@phansch.net>
Tue, 29 Jan 2019 07:19:05 +0000 (08:19 +0100)
clippy_lints/src/missing_const_for_fn.rs
clippy_lints/src/utils/mod.rs
tests/ui/missing_const_for_fn/cant_be_const.rs
tests/ui/missing_const_for_fn/could_be_const.rs
tests/ui/missing_const_for_fn/could_be_const.stderr

index 3e5c81484d542eb07b37a821dfb085c9240e3ec0..4e85d13332c16275818f8ee1367f751b6ffd3fe7 100644 (file)
@@ -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:**
 ///
 /// 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 @@
 ///
 /// ```rust
 /// fn new() -> Self {
-///     Self {
-///         random_number: 42
-///     }
+///     Self { random_number: 42 }
 /// }
 /// ```
 ///
@@ -48,9 +50,7 @@
 ///
 /// ```rust
 /// const fn new() -> Self {
-///     Self {
-///         random_number: 42
-///     }
+///     Self { random_number: 42 }
 /// }
 /// ```
 declare_clippy_lint! {
@@ -80,7 +80,7 @@ fn check_fn(
         _: &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.
@@ -97,7 +97,7 @@ fn check_fn(
                     return;
                 }
             },
-            _ => return
+            _ => return,
         }
 
         let def_id = cx.tcx.hir().local_def_id(node_id);
@@ -115,9 +115,13 @@ fn check_fn(
 
 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
 }
index f06a257b5ca48dac45ae4a58461a36875a6e5d15..32fe5e22dd54531ad06f6d65df7d84f40a7af90f 100644 (file)
@@ -354,11 +354,9 @@ pub fn method_chain_args<'a>(expr: &'a Expr, methods: &[&str]) -> Option<Vec<&'a
 ///
 /// This is either the usual `main` function or a custom function with the `#[start]` attribute.
 pub fn is_entrypoint_fn(fn_name: &str, attrs: &[ast::Attribute]) -> 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"
 }
index cfaf01cf3c11950e90473c1a4027544a4d04edd8..ede3724cc6b0dcaf471e1d1fb6ec4749b7ef7724 100644 (file)
@@ -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 @@ fn random_caller() -> u32 {
 // 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 @@ fn g() -> u32 {
 
 // 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
+}
index 3ba39711e8d6ae5baa7c2d8e62a3452264349d72..2c0a8e7a3c11fc95e46bacfc729231bd2eb4c956 100644 (file)
@@ -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) -> T {
index 593f9cf810ac665b1d9e5284d0b59174d8cc5091..22ea852905dacf1e4e1599d6a8ba0c673b96bfff 100644 (file)
@@ -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) -> T {
 LL | |     t