Skip to content

Commit 7e315c1

Browse files
committed
Use method chaining
1 parent 4080b02 commit 7e315c1

File tree

1 file changed

+19
-28
lines changed

1 file changed

+19
-28
lines changed

compiler/parser/python.lalrpop

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -360,15 +360,11 @@ IfStatement: ast::Stmt = {
360360
<location:@L> "if" <test:NamedExpressionTest> ":" <body:Suite> <s2:(@L "elif" NamedExpressionTest ":" Suite)*> <s3:("else" ":" Suite)?> => {
361361
// Determine last else:
362362
let mut last = s3.map(|s| s.2).unwrap_or_default();
363-
let end_location = if let Some(last) = last.last() {
364-
last.end_location
365-
} else {
366-
if let Some(last) = s2.last() {
367-
last.4.last().unwrap().end_location
368-
} else {
369-
body.last().unwrap().end_location
370-
}
371-
};
363+
let end_location = last
364+
.last()
365+
.map(|last| last.end_location)
366+
.or_else(|| s2.last().map(|last| last.4.last().unwrap().end_location))
367+
.unwrap_or_else(|| body.last().unwrap().end_location);
372368
// handle elif:
373369
for i in s2.into_iter().rev() {
374370
let x = ast::Stmt {
@@ -391,11 +387,10 @@ IfStatement: ast::Stmt = {
391387

392388
WhileStatement: ast::Stmt = {
393389
<location:@L> "while" <test:NamedExpressionTest> ":" <body:Suite> <s2:("else" ":" Suite)?> => {
394-
let end_location = if let Some(ref s) = s2 {
395-
s.2.last().unwrap().end_location
396-
} else {
397-
body.last().unwrap().end_location
398-
};
390+
let end_location = s2
391+
.as_ref()
392+
.map(|s| s.2.last().unwrap().end_location)
393+
.unwrap_or_else(|| body.last().unwrap().end_location);
399394
let orelse = s2.map(|s| s.2).unwrap_or_default();
400395
ast::Stmt {
401396
custom: (),
@@ -412,11 +407,11 @@ WhileStatement: ast::Stmt = {
412407

413408
ForStatement: ast::Stmt = {
414409
<location:@L> <is_async:"async"?> "for" <target:ExpressionList> "in" <iter:TestList> ":" <body:Suite> <s2:("else" ":" Suite)?> => {
415-
let end_location = if let Some(ref s) = s2 {
416-
s.2.last().unwrap().end_location.unwrap()
417-
} else {
418-
body.last().unwrap().end_location.unwrap()
419-
};
410+
let end_location = s2
411+
.as_ref()
412+
.map(|s| s.2.last().unwrap().end_location)
413+
.unwrap_or_else(|| body.last().unwrap().end_location)
414+
.unwrap();
420415
let orelse = s2.map(|s| s.2).unwrap_or_default();
421416
let target = Box::new(set_context(target, ast::ExprContext::Store));
422417
let iter = Box::new(iter);
@@ -434,15 +429,11 @@ TryStatement: ast::Stmt = {
434429
<location:@L> "try" ":" <body:Suite> <handlers:ExceptClause+> <else_suite:("else" ":" Suite)?> <finally:("finally" ":" Suite)?> <end_location:@R> => {
435430
let orelse = else_suite.map(|s| s.2).unwrap_or_default();
436431
let finalbody = finally.map(|s| s.2).unwrap_or_default();
437-
let end_location = if let Some(last) = finalbody.last() {
438-
last.end_location
439-
} else {
440-
if let Some(last) = orelse.last() {
441-
last.end_location
442-
} else {
443-
handlers.last().unwrap().end_location
444-
}
445-
};
432+
let end_location = finalbody
433+
.last()
434+
.map(|last| last.end_location)
435+
.or_else(|| orelse.last().map(|last| last.end_location))
436+
.unwrap_or_else(|| handlers.last().unwrap().end_location);
446437
ast::Stmt {
447438
custom: (),
448439
location,

0 commit comments

Comments
 (0)