Skip to content

Commit a2a357e

Browse files
committed
JOKE: Add lint to check for expressions that could be compile time
1 parent 6631a2c commit a2a357e

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5727,6 +5727,7 @@ Released 2018-09-13
57275727
[`misrefactored_assign_op`]: https://rust-lang.github.io/rust-clippy/master/index.html#misrefactored_assign_op
57285728
[`missing_assert_message`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_assert_message
57295729
[`missing_asserts_for_indexing`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_asserts_for_indexing
5730+
[`missing_const_for_expr`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_expr
57305731
[`missing_const_for_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn
57315732
[`missing_const_for_thread_local`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_thread_local
57325733
[`missing_docs_in_private_items`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_docs_in_private_items

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
513513
crate::mismatching_type_param_order::MISMATCHING_TYPE_PARAM_ORDER_INFO,
514514
crate::missing_assert_message::MISSING_ASSERT_MESSAGE_INFO,
515515
crate::missing_asserts_for_indexing::MISSING_ASSERTS_FOR_INDEXING_INFO,
516+
crate::missing_const_for_expr::MISSING_CONST_FOR_EXPR_INFO,
516517
crate::missing_const_for_fn::MISSING_CONST_FOR_FN_INFO,
517518
crate::missing_const_for_thread_local::MISSING_CONST_FOR_THREAD_LOCAL_INFO,
518519
crate::missing_doc::MISSING_DOCS_IN_PRIVATE_ITEMS_INFO,

clippy_lints/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![deny(clippy::missing_const_for_expr)]
12
#![feature(array_windows)]
23
#![feature(binary_heap_into_iter_sorted)]
34
#![feature(box_patterns)]
@@ -232,6 +233,7 @@ mod misc_early;
232233
mod mismatching_type_param_order;
233234
mod missing_assert_message;
234235
mod missing_asserts_for_indexing;
236+
mod missing_const_for_expr;
235237
mod missing_const_for_fn;
236238
mod missing_const_for_thread_local;
237239
mod missing_doc;
@@ -951,5 +953,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
951953
store.register_late_pass(|_| Box::new(manual_ignore_case_cmp::ManualIgnoreCaseCmp));
952954
store.register_late_pass(|_| Box::new(unnecessary_literal_bound::UnnecessaryLiteralBound));
953955
store.register_late_pass(move |_| Box::new(arbitrary_source_item_ordering::ArbitrarySourceItemOrdering::new(conf)));
956+
store.register_late_pass(|_| Box::new(missing_const_for_expr::MissingConstForExpr));
954957
// add lints here, do not remove this comment, it's used in `new_lint`
955958
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use std::ops::ControlFlow;
2+
3+
use clippy_utils::consts::ConstEvalCtxt;
4+
use clippy_utils::diagnostics::span_lint_and_sugg;
5+
use clippy_utils::source::snippet_opt;
6+
use clippy_utils::visitors::Descend;
7+
use rustc_errors::Applicability;
8+
use rustc_hir::def_id::LocalDefId;
9+
use rustc_hir::intravisit::FnKind;
10+
use rustc_hir::{Body, ExprKind, FnDecl};
11+
use rustc_lint::{LateContext, LateLintPass};
12+
use rustc_session::declare_lint_pass;
13+
use rustc_span::Span;
14+
15+
declare_clippy_lint! {
16+
/// ### What it does
17+
///
18+
/// ### Why restrict this?
19+
///
20+
/// ### Example
21+
/// ```no_run
22+
/// // example code where clippy issues a warning
23+
/// ```
24+
/// Use instead:
25+
/// ```no_run
26+
/// // example code which does not raise clippy warning
27+
/// ```
28+
#[clippy::version = "1.84.0"]
29+
pub MISSING_CONST_FOR_EXPR,
30+
restriction,
31+
"not the default lint description"
32+
}
33+
34+
declare_lint_pass!(MissingConstForExpr => [MISSING_CONST_FOR_EXPR]);
35+
36+
impl LateLintPass<'_> for MissingConstForExpr {
37+
fn check_fn(
38+
&mut self,
39+
cx: &LateContext<'_>,
40+
_: FnKind<'_>,
41+
_: &FnDecl<'_>,
42+
body: &Body<'_>,
43+
_: Span,
44+
_: LocalDefId,
45+
) {
46+
clippy_utils::visitors::for_each_expr_without_closures::<!, Descend>(body, |expr| {
47+
if !matches!(expr.kind, ExprKind::ConstBlock(_))
48+
&& ConstEvalCtxt::new(cx).eval(expr).is_some()
49+
&& let Some(snippet) = snippet_opt(cx, expr.span)
50+
{
51+
span_lint_and_sugg(
52+
cx,
53+
MISSING_CONST_FOR_EXPR,
54+
expr.span,
55+
"this expression could be inside a `const` block",
56+
"replace with",
57+
format!("const {{ {snippet} }}"),
58+
Applicability::MachineApplicable,
59+
);
60+
61+
ControlFlow::Continue(Descend::No)
62+
} else {
63+
ControlFlow::Continue(Descend::Yes)
64+
}
65+
});
66+
}
67+
}

tests/ui/missing_const_for_expr.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![warn(clippy::missing_const_for_expr)]
2+
3+
fn main() {
4+
// test code goes here
5+
}

0 commit comments

Comments
 (0)