From 530497b8a12602aadc66ef0bc34a4730b48c0a64 Mon Sep 17 00:00:00 2001 From: kraktus Date: Mon, 16 Jan 2023 14:02:20 +0100 Subject: [PATCH] [`unused_braces`] Lint multiline blocks as long as not in arms Currently the lint faces a severe limitation: since it only catches single-line block, running rustfmt beforehand will remove all occurences of it, because it breaks them into multiline blocks. We do not check match `Arm` for two reasons: - In case it does not use commas to separate arms, removing the block would result in a compilation error Example: ``` match expr { pat => {()} _ => println!("foo") } ``` - Do not lint multiline match arms used for formatting reasons ``` match expr { pat => { somewhat_long_expression } // ... } ``` Delete `unused-braces-lint` test The modified lint correctly provide a span in its suggestion. ```shell error: unnecessary braces around block return value --> /rust/src/test/rustdoc-ui/unused-braces-lint.rs:9:5 | LL | / { LL | | { | |________^ LL | use std; LL | } | __________^ LL | | } | |_____^ | note: the lint level is defined here --> /rust/src/test/rustdoc-ui/unused-braces-lint.rs:6:9 | LL | #![deny(unused_braces)] | ^^^^^^^^^^^^^ help: remove these braces | LL ~ { LL | use std; LL ~ } | ``` It is unclear to which extend https://github.com/rust-lang/rust/issues/70814 is still an issue, as the inital MCVE does not trigger the lint on stable either,[rust playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b6ff31a449c0b73a08daac8ee43b1fa6) Fix code with expanded `unused_braces` lint Also allow `unused_braces` on tests Mute `unused_braces` on `match_ast!` --- compiler/rustc_codegen_cranelift/src/base.rs | 12 ++--- .../src/debuginfo/metadata.rs | 32 ++++++------- compiler/rustc_codegen_ssa/src/mir/block.rs | 26 +++++----- compiler/rustc_expand/src/placeholders.rs | 8 ++-- compiler/rustc_lint/src/context.rs | 10 ++-- compiler/rustc_lint/src/unused.rs | 28 +++++++---- .../rustc_middle/src/ty/structural_impls.rs | 9 ++-- .../src/thir/pattern/check_match.rs | 4 +- compiler/rustc_save_analysis/src/lib.rs | 48 +++++++++---------- compiler/rustc_span/src/hygiene.rs | 8 ++-- .../src/traits/select/mod.rs | 16 ++++--- compiler/rustc_ty_utils/src/layout.rs | 7 ++- library/core/src/iter/adapters/step_by.rs | 6 +-- library/core/src/ptr/const_ptr.rs | 6 +-- library/core/src/ptr/mut_ptr.rs | 6 +-- library/std/src/collections/hash/map.rs | 4 +- library/std/src/io/stdio.rs | 4 +- library/test/src/lib.rs | 20 ++++---- src/librustdoc/clean/inline.rs | 17 ++----- .../clippy/tests/ui/manual_unwrap_or.fixed | 2 +- src/tools/clippy/tests/ui/manual_unwrap_or.rs | 2 +- ...edundant_pattern_matching_drop_order.fixed | 2 +- .../redundant_pattern_matching_drop_order.rs | 2 +- .../ide-completion/src/context/analysis.rs | 6 +-- .../rust-analyzer/crates/syntax/src/lib.rs | 5 +- tests/rustdoc-ui/unused-braces-lint.rs | 14 ------ tests/ui/drop/drop_order.rs | 2 + tests/ui/lint/unused_braces.fixed | 18 ++++++- tests/ui/lint/unused_braces.rs | 18 ++++++- tests/ui/nullable-pointer-iotareduction.rs | 2 + tests/ui/statics/static-promotion.rs | 2 + 31 files changed, 175 insertions(+), 171 deletions(-) delete mode 100644 tests/rustdoc-ui/unused-braces-lint.rs diff --git a/compiler/rustc_codegen_cranelift/src/base.rs b/compiler/rustc_codegen_cranelift/src/base.rs index d3a8c10657e8d..266b3b2a6722e 100644 --- a/compiler/rustc_codegen_cranelift/src/base.rs +++ b/compiler/rustc_codegen_cranelift/src/base.rs @@ -50,10 +50,10 @@ pub(crate) fn codegen_fn<'tcx>( let mir = tcx.instance_mir(instance.def); let _mir_guard = crate::PrintOnPanic(|| { let mut buf = Vec::new(); - with_no_trimmed_paths!({ + with_no_trimmed_paths!( rustc_middle::mir::pretty::write_mir_fn(tcx, mir, &mut |_, _| Ok(()), &mut buf) - .unwrap(); - }); + .unwrap() + ); String::from_utf8_lossy(&buf).into_owned() }); @@ -295,9 +295,9 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) { if fx.clif_comments.enabled() { let mut terminator_head = "\n".to_string(); - with_no_trimmed_paths!({ - bb_data.terminator().kind.fmt_head(&mut terminator_head).unwrap(); - }); + with_no_trimmed_paths!( + bb_data.terminator().kind.fmt_head(&mut terminator_head).unwrap() + ); let inst = fx.bcx.func.layout.last_inst(block).unwrap(); fx.add_comment(inst, terminator_head); } diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index b6eb5ee183fa3..63f1eaa53014c 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -465,27 +465,25 @@ pub fn type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll D _ => bug!("debuginfo: unexpected type in type_di_node(): {:?}", t), }; - { - if already_stored_in_typemap { - // Make sure that we really do have a `TypeMap` entry for the unique type ID. - let di_node_for_uid = - match debug_context(cx).type_map.di_node_for_unique_id(unique_type_id) { - Some(di_node) => di_node, - None => { - bug!( - "expected type debuginfo node for unique \ + if already_stored_in_typemap { + // Make sure that we really do have a `TypeMap` entry for the unique type ID. + let di_node_for_uid = match debug_context(cx).type_map.di_node_for_unique_id(unique_type_id) + { + Some(di_node) => di_node, + None => { + bug!( + "expected type debuginfo node for unique \ type ID '{:?}' to already be in \ the `debuginfo::TypeMap` but it \ was not.", - unique_type_id, - ); - } - }; + unique_type_id, + ); + } + }; - debug_assert_eq!(di_node_for_uid as *const _, di_node as *const _); - } else { - debug_context(cx).type_map.insert(unique_type_id, di_node); - } + debug_assert_eq!(di_node_for_uid as *const _, di_node as *const _); + } else { + debug_context(cx).type_map.insert(unique_type_id, di_node); } di_node diff --git a/compiler/rustc_codegen_ssa/src/mir/block.rs b/compiler/rustc_codegen_ssa/src/mir/block.rs index 2623a650e07b2..3c791b3dad2bf 100644 --- a/compiler/rustc_codegen_ssa/src/mir/block.rs +++ b/compiler/rustc_codegen_ssa/src/mir/block.rs @@ -678,21 +678,17 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> { MemUninitializedValid => !bx.tcx().permits_uninit_init(bx.param_env().and(layout)), }; Some(if do_panic { - let msg_str = with_no_visible_paths!({ - with_no_trimmed_paths!({ - if layout.abi.is_uninhabited() { - // Use this error even for the other intrinsics as it is more precise. - format!("attempted to instantiate uninhabited type `{}`", ty) - } else if intrinsic == ZeroValid { - format!("attempted to zero-initialize type `{}`, which is invalid", ty) - } else { - format!( - "attempted to leave type `{}` uninitialized, which is invalid", - ty - ) - } - }) - }); + let msg_str = with_no_visible_paths!(with_no_trimmed_paths!(if layout + .abi + .is_uninhabited() + { + // Use this error even for the other intrinsics as it is more precise. + format!("attempted to instantiate uninhabited type `{}`", ty) + } else if intrinsic == ZeroValid { + format!("attempted to zero-initialize type `{}`, which is invalid", ty) + } else { + format!("attempted to leave type `{}` uninitialized, which is invalid", ty) + })); let msg = bx.const_str(&msg_str); // Obtain the panic entry point. diff --git a/compiler/rustc_expand/src/placeholders.rs b/compiler/rustc_expand/src/placeholders.rs index 03bb5c1dfe45f..984e2777bd852 100644 --- a/compiler/rustc_expand/src/placeholders.rs +++ b/compiler/rustc_expand/src/placeholders.rs @@ -144,8 +144,8 @@ pub fn placeholder( span, is_placeholder: true, }]), - AstFragmentKind::GenericParams => AstFragment::GenericParams(smallvec![{ - ast::GenericParam { + AstFragmentKind::GenericParams => { + AstFragment::GenericParams(smallvec![ast::GenericParam { attrs: Default::default(), bounds: Default::default(), id, @@ -153,8 +153,8 @@ pub fn placeholder( is_placeholder: true, kind: ast::GenericParamKind::Lifetime, colon_span: None, - } - }]), + }]) + } AstFragmentKind::Params => AstFragment::Params(smallvec![ast::Param { attrs: Default::default(), id, diff --git a/compiler/rustc_lint/src/context.rs b/compiler/rustc_lint/src/context.rs index 8046cc21cea58..475b7b54bb2a3 100644 --- a/compiler/rustc_lint/src/context.rs +++ b/compiler/rustc_lint/src/context.rs @@ -1199,12 +1199,10 @@ impl<'tcx> LateContext<'tcx> { } // This shouldn't ever be needed, but just in case: - with_no_trimmed_paths!({ - Ok(vec![match trait_ref { - Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)), - None => Symbol::intern(&format!("<{}>", self_ty)), - }]) - }) + with_no_trimmed_paths!(Ok(vec![match trait_ref { + Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)), + None => Symbol::intern(&format!("<{}>", self_ty)), + }])) } fn path_append_impl( diff --git a/compiler/rustc_lint/src/unused.rs b/compiler/rustc_lint/src/unused.rs index 4c9b3df2dbd33..cf256d35e59ea 100644 --- a/compiler/rustc_lint/src/unused.rs +++ b/compiler/rustc_lint/src/unused.rs @@ -1116,15 +1116,6 @@ impl UnusedDelimLint for UnusedBraces { // - the block is not `unsafe` // - the block contains exactly one expression (do not lint `{ expr; }`) // - `followed_by_block` is true and the internal expr may contain a `{` - // - the block is not multiline (do not lint multiline match arms) - // ``` - // match expr { - // Pattern => { - // somewhat_long_expression - // } - // // ... - // } - // ``` // - the block has no attribute and was not created inside a macro // - if the block is an `anon_const`, the inner expr must be a literal // not created by a macro, i.e. do not lint on: @@ -1133,6 +1124,24 @@ impl UnusedDelimLint for UnusedBraces { // let _: A<{ 2 + 3 }>; // let _: A<{produces_literal!()}>; // ``` + // + // We do not check expression in `Arm` bodies: + // - if not using commas to separate arms, removing the block would result in a compilation error + // ``` + // match expr { + // pat => {()} + // _ => println!("foo") + // } + // ``` + // - multiline blocks can used for formatting + // ``` + // match expr { + // pat => { + // somewhat_long_expression + // } + // // ... + // } + // ``` // FIXME(const_generics): handle paths when #67075 is fixed. if let [stmt] = inner.stmts.as_slice() { if let ast::StmtKind::Expr(ref expr) = stmt.kind { @@ -1140,7 +1149,6 @@ impl UnusedDelimLint for UnusedBraces { && (ctx != UnusedDelimsCtx::AnonConst || (matches!(expr.kind, ast::ExprKind::Lit(_)) && !expr.span.from_expansion())) - && !cx.sess().source_map().is_multiline(value.span) && value.attrs.is_empty() && !value.span.from_expansion() && !inner.span.from_expansion() diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 2de886a3e817f..759eee9164b40 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -22,13 +22,13 @@ use std::sync::Arc; impl fmt::Debug for ty::TraitDef { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { ty::tls::with(|tcx| { - with_no_trimmed_paths!({ + with_no_trimmed_paths!( f.write_str( &FmtPrinter::new(tcx, Namespace::TypeNS) .print_def_path(self.def_id, &[])? .into_buffer(), ) - }) + ) }) } } @@ -36,13 +36,13 @@ impl fmt::Debug for ty::TraitDef { impl<'tcx> fmt::Debug for ty::AdtDef<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { ty::tls::with(|tcx| { - with_no_trimmed_paths!({ + with_no_trimmed_paths!( f.write_str( &FmtPrinter::new(tcx, Namespace::TypeNS) .print_def_path(self.did(), &[])? .into_buffer(), ) - }) + ) }) } } @@ -77,6 +77,7 @@ impl fmt::Debug for ty::BoundRegionKind { write!(f, "BrNamed({:?}, {})", did, name) } } + ty::BrEnv => write!(f, "BrEnv"), } } diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index 2640ca56b00e9..b66afa35e62d9 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -488,9 +488,9 @@ fn check_for_bindings_named_same_as_variants( }) { let variant_count = edef.variants().len(); - let ty_path = with_no_trimmed_paths!({ + let ty_path = with_no_trimmed_paths!( cx.tcx.def_path_str(edef.did()) - }); + ); cx.tcx.emit_spanned_lint( BINDINGS_WITH_VARIANT_NAME, p.hir_id, diff --git a/compiler/rustc_save_analysis/src/lib.rs b/compiler/rustc_save_analysis/src/lib.rs index a8d82de02b754..555ee2c82b333 100644 --- a/compiler/rustc_save_analysis/src/lib.rs +++ b/compiler/rustc_save_analysis/src/lib.rs @@ -964,35 +964,33 @@ pub fn process_crate( config: Option, mut handler: H, ) { - with_no_trimmed_paths!({ - tcx.dep_graph.with_ignore(|| { - info!("Dumping crate {}", cratename); - - // Privacy checking must be done outside of type inference; use a - // fallback in case effective visibilities couldn't have been correctly computed. - let effective_visibilities = match tcx.sess.compile_status() { - Ok(..) => tcx.effective_visibilities(()), - Err(..) => tcx.arena.alloc(EffectiveVisibilities::default()), - }; + with_no_trimmed_paths!(tcx.dep_graph.with_ignore(|| { + info!("Dumping crate {}", cratename); + + // Privacy checking must be done outside of type inference; use a + // fallback in case effective visibilities couldn't have been correctly computed. + let effective_visibilities = match tcx.sess.compile_status() { + Ok(..) => tcx.effective_visibilities(()), + Err(..) => tcx.arena.alloc(EffectiveVisibilities::default()), + }; - let save_ctxt = SaveContext { - tcx, - maybe_typeck_results: None, - effective_visibilities: &effective_visibilities, - span_utils: SpanUtils::new(&tcx.sess), - config: find_config(config), - impl_counter: Cell::new(0), - }; + let save_ctxt = SaveContext { + tcx, + maybe_typeck_results: None, + effective_visibilities: &effective_visibilities, + span_utils: SpanUtils::new(&tcx.sess), + config: find_config(config), + impl_counter: Cell::new(0), + }; - let mut visitor = DumpVisitor::new(save_ctxt); + let mut visitor = DumpVisitor::new(save_ctxt); - visitor.dump_crate_info(cratename); - visitor.dump_compilation_options(input, cratename); - visitor.process_crate(); + visitor.dump_crate_info(cratename); + visitor.dump_compilation_options(input, cratename); + visitor.process_crate(); - handler.save(&visitor.save_ctxt, &visitor.analysis()) - }) - }) + handler.save(&visitor.save_ctxt, &visitor.analysis()) + })) } fn find_config(supplied: Option) -> Config { diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index dee823eefde68..9aff186a6f51e 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -1332,11 +1332,9 @@ pub fn decode_syntax_context SyntaxContext let outer_ctxts = &context.remapped_ctxts; - // Ensure that the lock() temporary is dropped early - { - if let Some(ctxt) = outer_ctxts.lock().get(raw_id as usize).copied().flatten() { - return ctxt; - } + // the lock() temporary is dropped early + if let Some(ctxt) = outer_ctxts.lock().get(raw_id as usize).copied().flatten() { + return ctxt; } // Allocate and store SyntaxContext id *before* calling the decoder function, diff --git a/compiler/rustc_trait_selection/src/traits/select/mod.rs b/compiler/rustc_trait_selection/src/traits/select/mod.rs index 1d23634b6aacf..d26db48eecf3b 100644 --- a/compiler/rustc_trait_selection/src/traits/select/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/select/mod.rs @@ -2084,13 +2084,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { ty::Adt(def, substs) => { let sized_crit = def.sized_constraint(self.tcx()); // (*) binder moved here - Where(obligation.predicate.rebind({ - sized_crit - .0 - .iter() - .map(|ty| sized_crit.rebind(*ty).subst(self.tcx(), substs)) - .collect() - })) + Where( + obligation.predicate.rebind( + sized_crit + .0 + .iter() + .map(|ty| sized_crit.rebind(*ty).subst(self.tcx(), substs)) + .collect(), + ), + ) } ty::Alias(..) | ty::Param(_) => None, diff --git a/compiler/rustc_ty_utils/src/layout.rs b/compiler/rustc_ty_utils/src/layout.rs index 0f25579c7bfa1..3b37a7c8dac77 100644 --- a/compiler/rustc_ty_utils/src/layout.rs +++ b/compiler/rustc_ty_utils/src/layout.rs @@ -596,10 +596,9 @@ fn generator_saved_local_eligibility( } // Write down the order of our locals that will be promoted to the prefix. - { - for (idx, local) in ineligible_locals.iter().enumerate() { - assignments[local] = Ineligible(Some(idx as u32)); - } + + for (idx, local) in ineligible_locals.iter().enumerate() { + assignments[local] = Ineligible(Some(idx as u32)); } debug!("generator saved local assignments: {:?}", assignments); diff --git a/library/core/src/iter/adapters/step_by.rs b/library/core/src/iter/adapters/step_by.rs index 4252c34a0e0fc..186fcfafd7282 100644 --- a/library/core/src/iter/adapters/step_by.rs +++ b/library/core/src/iter/adapters/step_by.rs @@ -88,10 +88,8 @@ where // overflow handling loop { let mul = n.checked_mul(step); - { - if intrinsics::likely(mul.is_some()) { - return self.iter.nth(mul.unwrap() - 1); - } + if intrinsics::likely(mul.is_some()) { + return self.iter.nth(mul.unwrap() - 1); } let div_n = usize::MAX / n; let div_step = usize::MAX / step; diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index 16eb726f6f614..25fed226abf36 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -1349,10 +1349,8 @@ impl *const T { panic!("align_offset: align is not a power-of-two"); } - { - // SAFETY: `align` has been checked to be a power of 2 above - unsafe { align_offset(self, align) } - } + // SAFETY: `align` has been checked to be a power of 2 above + unsafe { align_offset(self, align) } } /// Returns whether the pointer is properly aligned for `T`. diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index 0a2f63e3ec6a5..2cb78becb306f 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -1617,10 +1617,8 @@ impl *mut T { panic!("align_offset: align is not a power-of-two"); } - { - // SAFETY: `align` has been checked to be a power of 2 above - unsafe { align_offset(self, align) } - } + // SAFETY: `align` has been checked to be a power of 2 above + unsafe { align_offset(self, align) } } /// Returns whether the pointer is properly aligned for `T`. diff --git a/library/std/src/collections/hash/map.rs b/library/std/src/collections/hash/map.rs index df490358827e7..3370a7e5481a8 100644 --- a/library/std/src/collections/hash/map.rs +++ b/library/std/src/collections/hash/map.rs @@ -3121,9 +3121,7 @@ impl RandomState { // iteration order allows a form of DOS attack. To counter that we // increment one of the seeds on every RandomState creation, giving // every corresponding HashMap a different iteration order. - thread_local!(static KEYS: Cell<(u64, u64)> = { - Cell::new(sys::hashmap_random_keys()) - }); + thread_local!(static KEYS: Cell<(u64, u64)> = Cell::new(sys::hashmap_random_keys())); KEYS.with(|keys| { let (k0, k1) = keys.get(); diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs index 14bfef4c7aad9..d7be5ef86bb0a 100644 --- a/library/std/src/io/stdio.rs +++ b/library/std/src/io/stdio.rs @@ -17,9 +17,7 @@ type LocalStream = Arc>>; thread_local! { /// Used by the test crate to capture the output of the print macros and panics. - static OUTPUT_CAPTURE: Cell> = { - Cell::new(None) - } + static OUTPUT_CAPTURE: Cell> = Cell::new(None) } /// Flag to indicate OUTPUT_CAPTURE is used. diff --git a/library/test/src/lib.rs b/library/test/src/lib.rs index 69fb529d7f563..c858e234a0661 100644 --- a/library/test/src/lib.rs +++ b/library/test/src/lib.rs @@ -120,20 +120,18 @@ pub fn test_main(args: &[String], tests: Vec, options: Option| { - if !info.can_unwind() { - std::mem::forget(std::io::stderr().lock()); - let mut stdout = ManuallyDrop::new(std::io::stdout().lock()); - if let Some(captured) = io::set_output_capture(None) { - if let Ok(data) = captured.lock() { - let _ = stdout.write_all(&data); - let _ = stdout.flush(); - } + let hook = Box::new(move |info: &'_ PanicInfo<'_>| { + if !info.can_unwind() { + std::mem::forget(std::io::stderr().lock()); + let mut stdout = ManuallyDrop::new(std::io::stdout().lock()); + if let Some(captured) = io::set_output_capture(None) { + if let Ok(data) = captured.lock() { + let _ = stdout.write_all(&data); + let _ = stdout.flush(); } } - builtin_panic_hook(info); } + builtin_panic_hook(info); }); panic::set_hook(hook); } diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index 6592692d8b214..c13be24fd0417 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -750,20 +750,13 @@ fn separate_supertrait_bounds( } pub(crate) fn record_extern_trait(cx: &mut DocContext<'_>, did: DefId) { - if did.is_local() { - return; - } - + if did.is_local() + || cx.external_traits.borrow().contains_key(&did) + || cx.active_extern_traits.contains(&did) { - if cx.external_traits.borrow().contains_key(&did) || cx.active_extern_traits.contains(&did) - { - return; - } - } - - { - cx.active_extern_traits.insert(did); + return; } + cx.active_extern_traits.insert(did); debug!("record_extern_trait: {:?}", did); let trait_ = build_external_trait(cx, did); diff --git a/src/tools/clippy/tests/ui/manual_unwrap_or.fixed b/src/tools/clippy/tests/ui/manual_unwrap_or.fixed index 7d68978216c9c..2c2c7b6652fa2 100644 --- a/src/tools/clippy/tests/ui/manual_unwrap_or.fixed +++ b/src/tools/clippy/tests/ui/manual_unwrap_or.fixed @@ -1,6 +1,6 @@ // run-rustfix #![allow(dead_code)] -#![allow(unused_variables, clippy::unnecessary_wraps)] +#![allow(unused_braces, unused_variables, clippy::unnecessary_wraps)] fn option_unwrap_or() { // int case diff --git a/src/tools/clippy/tests/ui/manual_unwrap_or.rs b/src/tools/clippy/tests/ui/manual_unwrap_or.rs index b937fe6f977e5..f5721408b26a6 100644 --- a/src/tools/clippy/tests/ui/manual_unwrap_or.rs +++ b/src/tools/clippy/tests/ui/manual_unwrap_or.rs @@ -1,6 +1,6 @@ // run-rustfix #![allow(dead_code)] -#![allow(unused_variables, clippy::unnecessary_wraps)] +#![allow(unused_braces, unused_variables, clippy::unnecessary_wraps)] fn option_unwrap_or() { // int case diff --git a/src/tools/clippy/tests/ui/redundant_pattern_matching_drop_order.fixed b/src/tools/clippy/tests/ui/redundant_pattern_matching_drop_order.fixed index ce3229f17591e..b7e894da12656 100644 --- a/src/tools/clippy/tests/ui/redundant_pattern_matching_drop_order.fixed +++ b/src/tools/clippy/tests/ui/redundant_pattern_matching_drop_order.fixed @@ -2,7 +2,7 @@ // Issue #5746 #![warn(clippy::redundant_pattern_matching)] -#![allow(clippy::if_same_then_else, clippy::equatable_if_let)] +#![allow(unused_braces, clippy::if_same_then_else, clippy::equatable_if_let)] use std::task::Poll::{Pending, Ready}; fn main() { diff --git a/src/tools/clippy/tests/ui/redundant_pattern_matching_drop_order.rs b/src/tools/clippy/tests/ui/redundant_pattern_matching_drop_order.rs index 29b8543cf473a..acee78b561db0 100644 --- a/src/tools/clippy/tests/ui/redundant_pattern_matching_drop_order.rs +++ b/src/tools/clippy/tests/ui/redundant_pattern_matching_drop_order.rs @@ -2,7 +2,7 @@ // Issue #5746 #![warn(clippy::redundant_pattern_matching)] -#![allow(clippy::if_same_then_else, clippy::equatable_if_let)] +#![allow(unused_braces, clippy::if_same_then_else, clippy::equatable_if_let)] use std::task::Poll::{Pending, Ready}; fn main() { diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/context/analysis.rs b/src/tools/rust-analyzer/crates/ide-completion/src/context/analysis.rs index e34824e22eac1..892a4a0ba248b 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/context/analysis.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/context/analysis.rs @@ -471,15 +471,13 @@ fn expected_type_and_name( ast::ParamList(_) => (None, None), ast::Stmt(_) => (None, None), ast::Item(_) => (None, None), - _ => { - match node.parent() { + _ => match node.parent() { Some(n) => { node = n; continue; }, None => (None, None), - } - }, + }, } }; } diff --git a/src/tools/rust-analyzer/crates/syntax/src/lib.rs b/src/tools/rust-analyzer/crates/syntax/src/lib.rs index 84c66b27e69fa..2bd2c52c1ae88 100644 --- a/src/tools/rust-analyzer/crates/syntax/src/lib.rs +++ b/src/tools/rust-analyzer/crates/syntax/src/lib.rs @@ -192,7 +192,10 @@ macro_rules! match_ast { $( $( $path:ident )::+ ($it:pat) => $res:expr, )* _ => $catch_all:expr $(,)? }) => {{ - $( if let Some($it) = $($path::)+cast($node.clone()) { $res } else )* + $( if let Some($it) = $($path::)+cast($node.clone()) { + #[allow(unused_braces)] // FIXME: remove and fix occurences on RA's repo with `cargo fix` + $res + } else )* { $catch_all } }}; } diff --git a/tests/rustdoc-ui/unused-braces-lint.rs b/tests/rustdoc-ui/unused-braces-lint.rs deleted file mode 100644 index be0e31e4be2ff..0000000000000 --- a/tests/rustdoc-ui/unused-braces-lint.rs +++ /dev/null @@ -1,14 +0,0 @@ -// check-pass - -// This tests the bug in #70814, where the unused_braces lint triggered on the following code -// without providing a span. - -#![deny(unused_braces)] - -fn main() { - { - { - use std; - } - } -} diff --git a/tests/ui/drop/drop_order.rs b/tests/ui/drop/drop_order.rs index 5ce1fd54a9e62..8a33b21aa4c45 100644 --- a/tests/ui/drop/drop_order.rs +++ b/tests/ui/drop/drop_order.rs @@ -2,6 +2,8 @@ // compile-flags: -Z validate-mir #![feature(let_chains)] +#![allow(unused_braces)] + use std::cell::RefCell; use std::convert::TryInto; diff --git a/tests/ui/lint/unused_braces.fixed b/tests/ui/lint/unused_braces.fixed index e691fb37e6c43..da84f800bbedb 100644 --- a/tests/ui/lint/unused_braces.fixed +++ b/tests/ui/lint/unused_braces.fixed @@ -37,7 +37,7 @@ fn main() { consume(7); //~^ WARN unnecessary braces - // Do not emit lint for multiline blocks. + // Do not emit lint for semicolon blocks. let _ = { 7 }; @@ -54,4 +54,20 @@ fn main() { // regression test for https://github.com/rust-lang/rust/issues/106899 return println!("!"); //~^ WARN unnecessary braces + + match Some(1) { + // Do not lint, removing the block without inserting a comma would be a compilation error + Some(_) => {()} + None => { + println!("Very very long expression that should not be linted for style"); + } + } + + match Some(2) { + Some(_) => 1, + None => { + // Do not lint, as defining what is a "long" line is a user preference + 2 + } + }; } diff --git a/tests/ui/lint/unused_braces.rs b/tests/ui/lint/unused_braces.rs index 0d260d2cbc93f..64671bb1ce19c 100644 --- a/tests/ui/lint/unused_braces.rs +++ b/tests/ui/lint/unused_braces.rs @@ -37,7 +37,7 @@ fn main() { consume({ 7 }); //~^ WARN unnecessary braces - // Do not emit lint for multiline blocks. + // Do not emit lint for semicolon blocks. let _ = { 7 }; @@ -54,4 +54,20 @@ fn main() { // regression test for https://github.com/rust-lang/rust/issues/106899 return { println!("!") }; //~^ WARN unnecessary braces + + match Some(1) { + // Do not lint, removing the block without inserting a comma would be a compilation error + Some(_) => {()} + None => { + println!("Very very long expression that should not be linted for style"); + } + } + + match Some(2) { + Some(_) => 1, + None => { + // Do not lint, as defining what is a "long" line is a user preference + 2 + } + }; } diff --git a/tests/ui/nullable-pointer-iotareduction.rs b/tests/ui/nullable-pointer-iotareduction.rs index d345fec811854..54eaadec7b657 100644 --- a/tests/ui/nullable-pointer-iotareduction.rs +++ b/tests/ui/nullable-pointer-iotareduction.rs @@ -8,6 +8,8 @@ // trying to get assert failure messages that at least identify which case // failed. +#![allow(unused_braces)] + enum E { Thing(isize, T), #[allow(unused_tuple_struct_fields)] Nothing((), ((), ()), [i8; 0]) } impl E { fn is_none(&self) -> bool { diff --git a/tests/ui/statics/static-promotion.rs b/tests/ui/statics/static-promotion.rs index b9eff469177e6..f6cde5360aa0f 100644 --- a/tests/ui/statics/static-promotion.rs +++ b/tests/ui/statics/static-promotion.rs @@ -8,6 +8,8 @@ // Literal values were previously promoted into local static values when // other global static variables are used. +#![allow(unused_braces)] + struct A(&'static T); struct B { x: &'static T,