Lints and suggestions for the Nix programming language
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(collapsible_let_in): use let-else and try-expression

+36 -35
+36 -35
lib/src/lints/collapsible_let_in.rs
··· 45 45 46 46 impl Rule for CollapsibleLetIn { 47 47 fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option<Report> { 48 - if let NodeOrToken::Node(node) = node 49 - && let Some(let_in_expr) = LetIn::cast(node.clone()) 50 - && let Some(body) = let_in_expr.body() 51 - && LetIn::cast(body.clone()).is_some() 52 - { 53 - let first_annotation = node.text_range(); 54 - let first_message = "This `let in` expression contains a nested `let in` expression"; 48 + let NodeOrToken::Node(node) = node else { 49 + return None; 50 + }; 55 51 56 - let second_annotation = body.text_range(); 57 - let second_message = "This `let in` expression is nested"; 52 + let let_in_expr = LetIn::cast(node.clone())?; 53 + let body = let_in_expr.body()?; 54 + 55 + LetIn::cast(body.clone())?; 58 56 59 - let replacement_at = { 60 - let start = body 61 - .siblings_with_tokens(Direction::Prev) 62 - .find(|elem| elem.kind() == SyntaxKind::TOKEN_IN)? 63 - .text_range() 64 - .start(); 65 - let end = body 66 - .descendants_with_tokens() 67 - .find(|elem| elem.kind() == SyntaxKind::TOKEN_LET)? 68 - .text_range() 69 - .end(); 70 - TextRange::new(start, end) 71 - }; 72 - let replacement = make::empty().node().clone(); 57 + let first_annotation = node.text_range(); 58 + let first_message = "This `let in` expression contains a nested `let in` expression"; 59 + 60 + let second_annotation = body.text_range(); 61 + let second_message = "This `let in` expression is nested"; 62 + 63 + let replacement_at = { 64 + let start = body 65 + .siblings_with_tokens(Direction::Prev) 66 + .find(|elem| elem.kind() == SyntaxKind::TOKEN_IN)? 67 + .text_range() 68 + .start(); 69 + let end = body 70 + .descendants_with_tokens() 71 + .find(|elem| elem.kind() == SyntaxKind::TOKEN_LET)? 72 + .text_range() 73 + .end(); 74 + TextRange::new(start, end) 75 + }; 76 + let replacement = make::empty().node().clone(); 73 77 74 - Some( 75 - self.report() 76 - .diagnostic(first_annotation, first_message) 77 - .suggest( 78 - second_annotation, 79 - second_message, 80 - Suggestion::new(replacement_at, replacement), 81 - ), 82 - ) 83 - } else { 84 - None 85 - } 78 + Some( 79 + self.report() 80 + .diagnostic(first_annotation, first_message) 81 + .suggest( 82 + second_annotation, 83 + second_message, 84 + Suggestion::new(replacement_at, replacement), 85 + ), 86 + ) 86 87 } 87 88 }