@@ -361,10 +361,11 @@ IfStatement: ast::Stmt = {
361361 // Determine last else:
362362 let mut last = s3.map(|s| s.2).unwrap_or_default();
363363 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);
364+ .last()
365+ .or_else(|| s2.last().and_then(|last| last.4.last()))
366+ .or_else(|| body.last())
367+ .unwrap()
368+ .end_location;
368369 // handle elif:
369370 for i in s2.into_iter().rev() {
370371 let x = ast::Stmt {
@@ -387,11 +388,12 @@ IfStatement: ast::Stmt = {
387388
388389WhileStatement: ast::Stmt = {
389390 <location:@L> "while" <test:NamedExpressionTest> ":" <body:Suite> <s2:("else" ":" Suite)?> => {
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);
394391 let orelse = s2.map(|s| s.2).unwrap_or_default();
392+ let end_location = orelse
393+ .last()
394+ .or_else(|| body.last())
395+ .unwrap()
396+ .end_location;
395397 ast::Stmt {
396398 custom: (),
397399 location,
@@ -407,12 +409,13 @@ WhileStatement: ast::Stmt = {
407409
408410ForStatement: ast::Stmt = {
409411 <location:@L> <is_async:"async"?> "for" <target:ExpressionList> "in" <iter:TestList> ":" <body:Suite> <s2:("else" ":" Suite)?> => {
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();
415412 let orelse = s2.map(|s| s.2).unwrap_or_default();
413+ let end_location = orelse
414+ .last()
415+ .or_else(|| body.last())
416+ .unwrap()
417+ .end_location
418+ .unwrap();
416419 let target = Box::new(set_context(target, ast::ExprContext::Store));
417420 let iter = Box::new(iter);
418421 let type_comment = None;
0 commit comments