Skip to content

Commit 75ce28a

Browse files
committed
Show auto-applicable correction warning for short circuiting in constants
1 parent 507ea97 commit 75ce28a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/librustc_mir/hair/cx/expr.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use rustc::hir;
2323
use rustc::hir::def_id::LocalDefId;
2424
use rustc::mir::{BorrowKind};
2525
use syntax_pos::Span;
26+
use syntax::errors::Applicability;
2627

2728
impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
2829
type Output = Expr<'tcx>;
@@ -373,6 +374,17 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
373374
// they can handle that kind of control-flow.
374375
(hir::BinOpKind::And, hir::Constness::Const) => {
375376
cx.control_flow_destroyed = true;
377+
cx.tcx.sess.struct_span_warn(
378+
op.span,
379+
"boolean short circuiting operators in constants do
380+
not actually short circuit. Thus new const eval features
381+
are not accessible in constants."
382+
).span_suggestion_with_applicability(
383+
op.span,
384+
"use a bit operator instead",
385+
"&".into(),
386+
Applicability::MachineApplicable,
387+
).emit();
376388
ExprKind::Binary {
377389
op: BinOp::BitAnd,
378390
lhs: lhs.to_ref(),
@@ -381,6 +393,17 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
381393
}
382394
(hir::BinOpKind::Or, hir::Constness::Const) => {
383395
cx.control_flow_destroyed = true;
396+
cx.tcx.sess.struct_span_warn(
397+
op.span,
398+
"boolean short circuiting operators in constants do
399+
not actually short circuit. Thus new const eval features
400+
are not accessible in constants."
401+
).span_suggestion_with_applicability(
402+
op.span,
403+
"use a bit operator instead",
404+
"|".into(),
405+
Applicability::MachineApplicable,
406+
).emit();
384407
ExprKind::Binary {
385408
op: BinOp::BitOr,
386409
lhs: lhs.to_ref(),

0 commit comments

Comments
 (0)