diff --git a/src/expr.rs b/src/expr.rs index db8d5b921..8216191c2 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1284,7 +1284,7 @@ pub(crate) mod parsing { if precedence == Precedence::Compare { if let Expr::Binary(lhs) = &lhs { if Precedence::of_binop(&lhs.op) == Precedence::Compare { - break; + return Err(input.error("comparison operators cannot be chained")); } } } @@ -1346,7 +1346,7 @@ pub(crate) mod parsing { if precedence == Precedence::Compare { if let Expr::Binary(lhs) = &lhs { if Precedence::of_binop(&lhs.op) == Precedence::Compare { - break; + return Err(input.error("comparison operators cannot be chained")); } } } diff --git a/tests/test_expr.rs b/tests/test_expr.rs index d20cce8d6..399972c42 100644 --- a/tests/test_expr.rs +++ b/tests/test_expr.rs @@ -642,6 +642,15 @@ fn test_assign_range_precedence() { syn::parse_str::("() .. () += ()").unwrap_err(); } +#[test] +fn test_chained_comparison() { + // https://github.com/dtolnay/syn/issues/1738 + let _ = syn::parse_str::("a = a < a <"); + + let err = syn::parse_str::("a < a < a").unwrap_err(); + assert_eq!("comparison operators cannot be chained", err.to_string()); +} + #[test] fn test_fixup() { struct FlattenParens;