1 #![allow(plugin_as_library)]
2 #![feature(rustc_private)]
7 extern crate rustc_front;
9 use rustc_front::hir::*;
10 use syntax::parse::token::InternedString;
12 use syntax::codemap::{Spanned, COMMAND_LINE_SP};
14 use syntax::ast::Lit_::*;
15 use syntax::ast::Lit_;
16 use syntax::ast::LitIntType::*;
17 use syntax::ast::StrStyle::*;
18 use syntax::ast::Sign::*;
20 use clippy::consts::{constant_simple, Constant};
21 use clippy::consts::Constant::*;
23 fn spanned<T>(t: T) -> Spanned<T> {
24 Spanned{ node: t, span: COMMAND_LINE_SP }
27 fn expr(n: Expr_) -> Expr {
31 span: COMMAND_LINE_SP,
36 fn lit(l: Lit_) -> Expr {
37 expr(ExprLit(P(spanned(l))))
40 fn binop(op: BinOp_, l: Expr, r: Expr) -> Expr {
41 expr(ExprBinary(spanned(op), P(l), P(r)))
44 fn check(expect: Constant, expr: &Expr) {
45 assert_eq!(Some(expect), constant_simple(expr))
48 const TRUE : Constant = ConstantBool(true);
49 const FALSE : Constant = ConstantBool(false);
50 const ZERO : Constant = ConstantInt(0, UnsuffixedIntLit(Plus));
51 const ONE : Constant = ConstantInt(1, UnsuffixedIntLit(Plus));
52 const TWO : Constant = ConstantInt(2, UnsuffixedIntLit(Plus));
56 check(TRUE, &lit(LitBool(true)));
57 check(FALSE, &lit(LitBool(false)));
58 check(ZERO, &lit(LitInt(0, UnsuffixedIntLit(Plus))));
59 check(ConstantStr("cool!".into(), CookedStr), &lit(LitStr(
60 InternedString::new("cool!"), CookedStr)));
65 check(TRUE, &binop(BiOr, lit(LitBool(false)), lit(LitBool(true))));
66 check(FALSE, &binop(BiAnd, lit(LitBool(false)), lit(LitBool(true))));
68 let litzero = lit(LitInt(0, UnsuffixedIntLit(Plus)));
69 let litone = lit(LitInt(1, UnsuffixedIntLit(Plus)));
70 check(TRUE, &binop(BiEq, litzero.clone(), litzero.clone()));
71 check(TRUE, &binop(BiGe, litzero.clone(), litzero.clone()));
72 check(TRUE, &binop(BiLe, litzero.clone(), litzero.clone()));
73 check(FALSE, &binop(BiNe, litzero.clone(), litzero.clone()));
74 check(FALSE, &binop(BiGt, litzero.clone(), litzero.clone()));
75 check(FALSE, &binop(BiLt, litzero.clone(), litzero.clone()));
77 check(ZERO, &binop(BiAdd, litzero.clone(), litzero.clone()));
78 check(TWO, &binop(BiAdd, litone.clone(), litone.clone()));
79 check(ONE, &binop(BiSub, litone.clone(), litzero.clone()));
80 check(ONE, &binop(BiMul, litone.clone(), litone.clone()));
81 check(ONE, &binop(BiDiv, litone.clone(), litone.clone()));