mirror of
				https://github.com/rust-lang/rust-analyzer.git
				synced 2025-11-03 13:13:18 +00:00 
			
		
		
		
	Remove some unwraps in add_new
This commit is contained in:
		
							parent
							
								
									169fe4932f
								
							
						
					
					
						commit
						03fe6b38b8
					
				@ -139,47 +139,44 @@ fn find_struct_impl(
 | 
			
		||||
 | 
			
		||||
    let struct_ty = {
 | 
			
		||||
        let src = InFile { file_id: ctx.frange.file_id.into(), value: strukt.clone() };
 | 
			
		||||
        hir::Struct::from_source(db, src).unwrap().ty(db)
 | 
			
		||||
        hir::Struct::from_source(db, src)?.ty(db)
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    let mut found_new_fn = false;
 | 
			
		||||
 | 
			
		||||
    let block = module.descendants().filter_map(ast::ImplBlock::cast).find(|impl_blk| {
 | 
			
		||||
        if found_new_fn {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    let block = module.descendants().filter_map(ast::ImplBlock::cast).find_map(|impl_blk| {
 | 
			
		||||
        let src = InFile { file_id: ctx.frange.file_id.into(), value: impl_blk.clone() };
 | 
			
		||||
        let blk = hir::ImplBlock::from_source(db, src).unwrap();
 | 
			
		||||
        let blk = hir::ImplBlock::from_source(db, src)?;
 | 
			
		||||
 | 
			
		||||
        let same_ty = blk.target_ty(db) == struct_ty;
 | 
			
		||||
        let not_trait_impl = blk.target_trait(db).is_none();
 | 
			
		||||
 | 
			
		||||
        if !(same_ty && not_trait_impl) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        found_new_fn = has_new_fn(impl_blk);
 | 
			
		||||
        true
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    if found_new_fn {
 | 
			
		||||
            None
 | 
			
		||||
        } else {
 | 
			
		||||
        Some(block)
 | 
			
		||||
            Some(impl_blk)
 | 
			
		||||
        }
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    if let Some(ref impl_blk) = block {
 | 
			
		||||
        if has_new_fn(impl_blk) {
 | 
			
		||||
            return None;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Some(block)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn has_new_fn(imp: &ast::ImplBlock) -> bool {
 | 
			
		||||
    if let Some(il) = imp.item_list() {
 | 
			
		||||
        for item in il.impl_items() {
 | 
			
		||||
            if let ast::ImplItem::FnDef(f) = item {
 | 
			
		||||
                if f.name().unwrap().text().eq_ignore_ascii_case("new") {
 | 
			
		||||
                if let Some(name) = f.name() {
 | 
			
		||||
                    if name.text().eq_ignore_ascii_case("new") {
 | 
			
		||||
                        return true;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user