Skip to content
Draft
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
32 changes: 31 additions & 1 deletion src/implementation/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use proc_macro2::{Spacing, TokenStream, TokenTree};
use syn::{Expr, ExprLit, Lit};
use syn::{spanned::Spanned, Expr, ExprLit, Lit};

/// Parse attributes into a list of expression and an optional description of
/// the assert
Expand Down Expand Up @@ -42,9 +42,39 @@ pub(crate) fn parse_attributes(
segments_stream.pop();
}

for cond in &mut conds {
warn_on_false_literal(cond);
}

(conds, segments_stream, desc)
}

fn warn_on_false_literal(expr: &mut Expr) {
let Expr::Lit(ExprLit {
lit: Lit::Bool(lit),
..
}) = expr
else {
return;
};

if lit.value {
return;
}

let span = expr.span();
let toks = quote::quote_spanned! { span=>
{
#[deprecated(note = "contract predicate is always false")]
const __CONTRACTS_FALSE_PREDICATE: bool = false;

__CONTRACTS_FALSE_PREDICATE
}
};

*expr = syn::parse2(toks).unwrap();
}

// This function rewrites a list of TokenTrees so that the "pseudooperator" for
// implication `==>` gets transformed into an `if` expression.
//
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/fail/always_false_predicate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![deny(deprecated)]

use contracts::ensures;

#[ensures(false)]
fn checked() {}

fn main() {
checked();
}
11 changes: 11 additions & 0 deletions tests/ui/fail/always_false_predicate.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: use of deprecated constant `checked::__CONTRACTS_FALSE_PREDICATE`: contract predicate is always false
--> tests/ui/fail/always_false_predicate.rs:5:11
|
5 | #[ensures(false)]
| ^^^^^
|
note: the lint level is defined here
--> tests/ui/fail/always_false_predicate.rs:1:9
|
1 | #![deny(deprecated)]
| ^^^^^^^^^^
Loading