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(eta_reduction): let-else and try-expression

+32 -24
+32 -24
lib/src/lints/eta_reduction.rs
··· 42 42 43 43 impl Rule for EtaReduction { 44 44 fn validate(&self, node: &SyntaxElement, _sess: &SessionInfo) -> Option<Report> { 45 - if let NodeOrToken::Node(node) = node 46 - && let Some(lambda_expr) = Lambda::cast(node.clone()) 47 - && let Some(arg_node) = lambda_expr.arg() 48 - && let Some(arg) = Ident::cast(arg_node) 49 - && let Some(body_node) = lambda_expr.body() 50 - && let Some(body) = Apply::cast(body_node) 51 - && let Some(value_node) = body.value() 52 - && let Some(value) = Ident::cast(value_node) 53 - && arg.as_str() == value.as_str() 54 - && let Some(lambda_node) = body.lambda() 55 - && !mentions_ident(&arg, &lambda_node) 56 - // lambda body should be no more than a single Ident to 57 - // retain code readability 58 - && let Some(_) = Ident::cast(lambda_node) 59 - { 60 - let at = node.text_range(); 61 - let replacement = body.lambda()?; 62 - let message = format!("Found eta-reduction: `{}`", replacement.text()); 63 - Some( 64 - self.report() 65 - .suggest(at, message, Suggestion::new(at, replacement)), 66 - ) 67 - } else { 68 - None 45 + let NodeOrToken::Node(node) = node else { 46 + return None; 47 + }; 48 + 49 + let lambda_expr = Lambda::cast(node.clone())?; 50 + let arg_node = lambda_expr.arg()?; 51 + let arg = Ident::cast(arg_node)?; 52 + let body_node = lambda_expr.body()?; 53 + let body = Apply::cast(body_node)?; 54 + let value_node = body.value()?; 55 + let value = Ident::cast(value_node)?; 56 + 57 + if arg.as_str() != value.as_str() { 58 + return None; 69 59 } 60 + 61 + let lambda_node = body.lambda()?; 62 + 63 + if mentions_ident(&arg, &lambda_node) { 64 + return None; 65 + } 66 + 67 + // lambda body should be no more than a single Ident to 68 + // retain code readability 69 + Ident::cast(lambda_node)?; 70 + 71 + let at = node.text_range(); 72 + let replacement = body.lambda()?; 73 + let message = format!("Found eta-reduction: `{}`", replacement.text()); 74 + Some( 75 + self.report() 76 + .suggest(at, message, Suggestion::new(at, replacement)), 77 + ) 70 78 } 71 79 } 72 80