Skip to content

Add a lint for unused const fn results #50805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/librustc_lint/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
UNUSED_DOC_COMMENT,
UNUSED_EXTERN_CRATES,
UNUSED_FEATURES,
UNUSED_PARENS);
UNUSED_PARENS,
UNUSED_CONST_FN_RESULTS);

add_lint_group!(sess,
"rust_2018_idioms",
Expand Down
12 changes: 11 additions & 1 deletion src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ declare_lint! {
"unused result of a type flagged as #[must_use]"
}

declare_lint! {
pub UNUSED_CONST_FN_RESULTS,
Warn,
"unused result of a const fn invocation in a statement"
}

declare_lint! {
pub UNUSED_RESULTS,
Allow,
Expand All @@ -42,7 +48,7 @@ pub struct UnusedResults;

impl LintPass for UnusedResults {
fn get_lints(&self) -> LintArray {
lint_array!(UNUSED_MUST_USE, UNUSED_RESULTS)
lint_array!(UNUSED_MUST_USE, UNUSED_CONST_FN_RESULTS, UNUSED_RESULTS)
}
}

Expand Down Expand Up @@ -95,6 +101,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
if let Some(def) = maybe_def {
let def_id = def.def_id();
fn_warned = check_must_use(cx, def_id, s.span, "return value of ");
if cx.tcx.is_const_fn(def_id) {
cx.span_lint(UNUSED_CONST_FN_RESULTS, s.span,
"unused const fn result");
}
}
let must_use_op = match expr.node {
// Hardcoding operators here seemed more expedient than the
Expand Down
25 changes: 25 additions & 0 deletions src/test/ui/unused-const-fn-result.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(const_fn,test)]
#![deny(unused_const_fn_results)]

extern crate test;

use test::black_box;

const fn identity<T>(v: T) -> T {
v
}

fn main() {
identity(42); //~ ERROR unused const fn result [unused_const_fn_results]
identity(black_box(42)); //~ ERROR unused const fn result [unused_const_fn_results]
}
20 changes: 20 additions & 0 deletions src/test/ui/unused-const-fn-result.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: unused const fn result
--> $DIR/unused-const-fn-result.rs:23:5
|
LL | identity(42); //~ ERROR unused const fn result [unused_const_fn_results]
| ^^^^^^^^^^^^^
|
note: lint level defined here
--> $DIR/unused-const-fn-result.rs:12:9
|
LL | #![deny(unused_const_fn_results)]
| ^^^^^^^^^^^^^^^^^^^^^^^

error: unused const fn result
--> $DIR/unused-const-fn-result.rs:24:5
|
LL | identity(black_box(42)); //~ ERROR unused const fn result [unused_const_fn_results]
| ^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors