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
///
/// ```rust
/// fn new() -> Self {
-/// Self {
-/// random_number: 42
-/// }
+/// Self { random_number: 42 }
/// }
/// ```
///
///
/// ```rust
/// const fn new() -> Self {
-/// Self {
-/// random_number: 42
-/// }
+/// Self { random_number: 42 }
/// }
/// ```
declare_clippy_lint! {
_: &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.
return;
}
},
- _ => return
+ _ => return,
}
let def_id = cx.tcx.hir().local_def_id(node_id);
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
}
///
/// 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"
}
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 {
// refer to a static variable
fn get_y() -> u32 {
Y
- //~^ ERROR E0013
+ //~^ ERROR E0013
}
// Also main should not be suggested to be made const
// 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
+}
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 {
}
// Could be const
-unsafe fn four() -> i32 { 4 }
+unsafe fn four() -> i32 {
+ 4
+}
// Could also be const
fn generic<T>(t: T) -> T {
--> $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
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