cargo clippy --fix

This commit is contained in:
Lukas Wirth 2025-07-31 10:30:22 +02:00
parent 423c7dd23a
commit 8ce30264c8
186 changed files with 3056 additions and 3314 deletions

View file

@ -89,11 +89,11 @@ impl IndentLevel {
_ => None,
});
for token in tokens {
if let Some(ws) = ast::Whitespace::cast(token) {
if ws.text().contains('\n') {
let new_ws = make::tokens::whitespace(&format!("{}{self}", ws.syntax()));
ted::replace(ws.syntax(), &new_ws);
}
if let Some(ws) = ast::Whitespace::cast(token)
&& ws.text().contains('\n')
{
let new_ws = make::tokens::whitespace(&format!("{}{self}", ws.syntax()));
ted::replace(ws.syntax(), &new_ws);
}
}
}
@ -122,13 +122,13 @@ impl IndentLevel {
_ => None,
});
for token in tokens {
if let Some(ws) = ast::Whitespace::cast(token) {
if ws.text().contains('\n') {
let new_ws = make::tokens::whitespace(
&ws.syntax().text().replace(&format!("\n{self}"), "\n"),
);
ted::replace(ws.syntax(), &new_ws);
}
if let Some(ws) = ast::Whitespace::cast(token)
&& ws.text().contains('\n')
{
let new_ws = make::tokens::whitespace(
&ws.syntax().text().replace(&format!("\n{self}"), "\n"),
);
ted::replace(ws.syntax(), &new_ws);
}
}
}

View file

@ -383,10 +383,10 @@ impl ast::GenericParamList {
impl ast::WhereClause {
pub fn add_predicate(&self, predicate: ast::WherePred) {
if let Some(pred) = self.predicates().last() {
if !pred.syntax().siblings_with_tokens(Direction::Next).any(|it| it.kind() == T![,]) {
ted::append_child_raw(self.syntax(), make::token(T![,]));
}
if let Some(pred) = self.predicates().last()
&& !pred.syntax().siblings_with_tokens(Direction::Next).any(|it| it.kind() == T![,])
{
ted::append_child_raw(self.syntax(), make::token(T![,]));
}
ted::append_child(self.syntax(), predicate.syntax());
}
@ -744,10 +744,10 @@ impl ast::LetStmt {
}
if let Some(existing_ty) = self.ty() {
if let Some(sibling) = existing_ty.syntax().prev_sibling_or_token() {
if sibling.kind() == SyntaxKind::WHITESPACE {
ted::remove(sibling);
}
if let Some(sibling) = existing_ty.syntax().prev_sibling_or_token()
&& sibling.kind() == SyntaxKind::WHITESPACE
{
ted::remove(sibling);
}
ted::remove(existing_ty.syntax());
@ -823,19 +823,18 @@ impl ast::RecordExprField {
return;
}
// this is a shorthand
if let Some(ast::Expr::PathExpr(path_expr)) = self.expr() {
if let Some(path) = path_expr.path() {
if let Some(name_ref) = path.as_single_name_ref() {
path_expr.syntax().detach();
let children = vec![
name_ref.syntax().clone().into(),
ast::make::token(T![:]).into(),
ast::make::tokens::single_space().into(),
expr.syntax().clone().into(),
];
ted::insert_all_raw(Position::last_child_of(self.syntax()), children);
}
}
if let Some(ast::Expr::PathExpr(path_expr)) = self.expr()
&& let Some(path) = path_expr.path()
&& let Some(name_ref) = path.as_single_name_ref()
{
path_expr.syntax().detach();
let children = vec![
name_ref.syntax().clone().into(),
ast::make::token(T![:]).into(),
ast::make::tokens::single_space().into(),
expr.syntax().clone().into(),
];
ted::insert_all_raw(Position::last_child_of(self.syntax()), children);
}
}
}

View file

@ -276,19 +276,19 @@ impl Expr {
}
// Not every expression can be followed by `else` in the `let-else`
if let Some(ast::Stmt::LetStmt(e)) = stmt {
if e.let_else().is_some() {
match self {
BinExpr(e)
if e.op_kind()
.map(|op| matches!(op, BinaryOp::LogicOp(_)))
.unwrap_or(false) =>
{
return true;
}
_ if self.clone().trailing_brace().is_some() => return true,
_ => {}
if let Some(ast::Stmt::LetStmt(e)) = stmt
&& e.let_else().is_some()
{
match self {
BinExpr(e)
if e.op_kind()
.map(|op| matches!(op, BinaryOp::LogicOp(_)))
.unwrap_or(false) =>
{
return true;
}
_ if self.clone().trailing_brace().is_some() => return true,
_ => {}
}
}

View file

@ -626,10 +626,10 @@ mod tests {
if let Some(ret_ty) = parent_fn.ret_type() {
editor.delete(ret_ty.syntax().clone());
if let Some(SyntaxElement::Token(token)) = ret_ty.syntax().next_sibling_or_token() {
if token.kind().is_trivia() {
editor.delete(token);
}
if let Some(SyntaxElement::Token(token)) = ret_ty.syntax().next_sibling_or_token()
&& token.kind().is_trivia()
{
editor.delete(token);
}
}

View file

@ -90,15 +90,15 @@ pub fn insert_raw(position: Position, elem: impl Element) {
insert_all_raw(position, vec![elem.syntax_element()]);
}
pub fn insert_all(position: Position, mut elements: Vec<SyntaxElement>) {
if let Some(first) = elements.first() {
if let Some(ws) = ws_before(&position, first) {
elements.insert(0, ws.into());
}
if let Some(first) = elements.first()
&& let Some(ws) = ws_before(&position, first)
{
elements.insert(0, ws.into());
}
if let Some(last) = elements.last() {
if let Some(ws) = ws_after(&position, last) {
elements.push(ws.into());
}
if let Some(last) = elements.last()
&& let Some(ws) = ws_after(&position, last)
{
elements.push(ws.into());
}
insert_all_raw(position, elements);
}
@ -165,20 +165,22 @@ fn ws_before(position: &Position, new: &SyntaxElement) -> Option<SyntaxToken> {
PositionRepr::After(it) => it,
};
if prev.kind() == T!['{'] && new.kind() == SyntaxKind::USE {
if let Some(item_list) = prev.parent().and_then(ast::ItemList::cast) {
let mut indent = IndentLevel::from_element(&item_list.syntax().clone().into());
indent.0 += 1;
return Some(make::tokens::whitespace(&format!("\n{indent}")));
}
if prev.kind() == T!['{']
&& new.kind() == SyntaxKind::USE
&& let Some(item_list) = prev.parent().and_then(ast::ItemList::cast)
{
let mut indent = IndentLevel::from_element(&item_list.syntax().clone().into());
indent.0 += 1;
return Some(make::tokens::whitespace(&format!("\n{indent}")));
}
if prev.kind() == T!['{'] && ast::Stmt::can_cast(new.kind()) {
if let Some(stmt_list) = prev.parent().and_then(ast::StmtList::cast) {
let mut indent = IndentLevel::from_element(&stmt_list.syntax().clone().into());
indent.0 += 1;
return Some(make::tokens::whitespace(&format!("\n{indent}")));
}
if prev.kind() == T!['{']
&& ast::Stmt::can_cast(new.kind())
&& let Some(stmt_list) = prev.parent().and_then(ast::StmtList::cast)
{
let mut indent = IndentLevel::from_element(&stmt_list.syntax().clone().into());
indent.0 += 1;
return Some(make::tokens::whitespace(&format!("\n{indent}")));
}
ws_between(prev, new)

View file

@ -142,50 +142,50 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec<SyntaxError>) {
match literal.kind() {
ast::LiteralKind::String(s) => {
if !s.is_raw() {
if let Some(without_quotes) = unquote(text, 1, '"') {
unescape_str(without_quotes, |range, char| {
if let Err(err) = char {
push_err(1, range.start, err);
}
});
}
if !s.is_raw()
&& let Some(without_quotes) = unquote(text, 1, '"')
{
unescape_str(without_quotes, |range, char| {
if let Err(err) = char {
push_err(1, range.start, err);
}
});
}
}
ast::LiteralKind::ByteString(s) => {
if !s.is_raw() {
if let Some(without_quotes) = unquote(text, 2, '"') {
unescape_byte_str(without_quotes, |range, char| {
if let Err(err) = char {
push_err(1, range.start, err);
}
});
}
if !s.is_raw()
&& let Some(without_quotes) = unquote(text, 2, '"')
{
unescape_byte_str(without_quotes, |range, char| {
if let Err(err) = char {
push_err(1, range.start, err);
}
});
}
}
ast::LiteralKind::CString(s) => {
if !s.is_raw() {
if let Some(without_quotes) = unquote(text, 2, '"') {
unescape_c_str(without_quotes, |range, char| {
if let Err(err) = char {
push_err(1, range.start, err);
}
});
}
if !s.is_raw()
&& let Some(without_quotes) = unquote(text, 2, '"')
{
unescape_c_str(without_quotes, |range, char| {
if let Err(err) = char {
push_err(1, range.start, err);
}
});
}
}
ast::LiteralKind::Char(_) => {
if let Some(without_quotes) = unquote(text, 1, '\'') {
if let Err(err) = unescape_char(without_quotes) {
push_err(1, 0, err);
}
if let Some(without_quotes) = unquote(text, 1, '\'')
&& let Err(err) = unescape_char(without_quotes)
{
push_err(1, 0, err);
}
}
ast::LiteralKind::Byte(_) => {
if let Some(without_quotes) = unquote(text, 2, '\'') {
if let Err(err) = unescape_byte(without_quotes) {
push_err(2, 0, err);
}
if let Some(without_quotes) = unquote(text, 2, '\'')
&& let Err(err) = unescape_byte(without_quotes)
{
push_err(2, 0, err);
}
}
ast::LiteralKind::IntNumber(_)
@ -224,14 +224,14 @@ pub(crate) fn validate_block_structure(root: &SyntaxNode) {
}
fn validate_numeric_name(name_ref: Option<ast::NameRef>, errors: &mut Vec<SyntaxError>) {
if let Some(int_token) = int_token(name_ref) {
if int_token.text().chars().any(|c| !c.is_ascii_digit()) {
errors.push(SyntaxError::new(
"Tuple (struct) field access is only allowed through \
if let Some(int_token) = int_token(name_ref)
&& int_token.text().chars().any(|c| !c.is_ascii_digit())
{
errors.push(SyntaxError::new(
"Tuple (struct) field access is only allowed through \
decimal integers with no underscores or suffix",
int_token.text_range(),
));
}
int_token.text_range(),
));
}
fn int_token(name_ref: Option<ast::NameRef>) -> Option<SyntaxToken> {
@ -285,13 +285,13 @@ fn validate_path_keywords(segment: ast::PathSegment, errors: &mut Vec<SyntaxErro
token.text_range(),
));
}
} else if let Some(token) = segment.crate_token() {
if !is_path_start || use_prefix(path).is_some() {
errors.push(SyntaxError::new(
"The `crate` keyword is only allowed as the first segment of a path",
token.text_range(),
));
}
} else if let Some(token) = segment.crate_token()
&& (!is_path_start || use_prefix(path).is_some())
{
errors.push(SyntaxError::new(
"The `crate` keyword is only allowed as the first segment of a path",
token.text_range(),
));
}
fn use_prefix(mut path: ast::Path) -> Option<ast::Path> {