@samitouri / QOSamiQemu / commits / 858d15b9e9

minikconf: replace else with early return and avoid unnecessary else

Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Paolo Bonzini committed Apr 30, 2026 at 10:59 UTC 858d15b9e94c37f051de1a724a863c12e34cf216
1 file changed +40 -39
scripts/minikconf.py
+40 -39
@@ -432,24 +432,24 @@ class KconfigParser:
432
433 # var: ID
434 def parse_var(self):
435 - if self.tok == TOK_ID:
436 - val = self.val
437 - self.get_token()
438 - return self.data.do_var(val)
439 - else:
435 + if self.tok != TOK_ID:
436 raise KconfigParserError(self, 'Expected identifier')
437 + val = self.val
438 + assert val is not None
439 + self.get_token()
440 + return self.data.do_var(val)
441
442 # assignment_var: ID (starting with "CONFIG_")
443 def parse_assignment_var(self):
444 - if self.tok == TOK_ID:
445 - val = self.val
446 - if not val.startswith("CONFIG_"):
447 - raise KconfigParserError(self,
448 - 'Expected identifier starting with "CONFIG_"', TOK_NONE)
449 - self.get_token()
450 - return self.data.do_var(val[7:])
451 - else:
444 + if self.tok != TOK_ID:
445 raise KconfigParserError(self, 'Expected identifier')
446 + val = self.val
447 + assert val is not None
448 + if not val.startswith("CONFIG_"):
449 + raise KconfigParserError(self,
450 + 'Expected identifier starting with "CONFIG_"', TOK_NONE)
451 + self.get_token()
452 + return self.data.do_var(val[7:])
453
454 # assignment: var EQUAL y_or_n
455 def parse_assignment(self):
@@ -497,11 +497,10 @@ class KconfigParser:
497 # condition: IF expr
498 # | empty
499 def parse_condition(self):
500 - if self.tok == TOK_IF:
501 - self.get_token()
502 - return self.parse_expr()
503 - else:
500 + if self.tok != TOK_IF:
501 return None
502 + self.get_token()
503 + return self.parse_expr()
504
505 # property: DEFAULT y_or_n condition
506 # | DEPENDS ON expr
@@ -606,41 +605,41 @@ class KconfigParser:
605 if ch == '#':
606 self.cursor = self.src.find('\n', self.cursor)
607 return None
609 - elif ch == '=':
608 + if ch == '=':
609 return TOK_EQUAL
611 - elif ch == '(':
610 + if ch == '(':
611 return TOK_LPAREN
613 - elif ch == ')':
612 + if ch == ')':
613 return TOK_RPAREN
615 - elif ch == '&' and self.src[self.pos+1] == '&':
614 + if ch == '&' and self.src[self.pos+1] == '&':
615 self.cursor += 1
616 return TOK_AND
618 - elif ch == '|' and self.src[self.pos+1] == '|':
617 + if ch == '|' and self.src[self.pos+1] == '|':
618 self.cursor += 1
619 return TOK_OR
621 - elif ch == '!':
620 + if ch == '!':
621 return TOK_NOT
623 - elif ch == 'd' and self.check_keyword("epends"):
622 + if ch == 'd' and self.check_keyword("epends"):
623 return TOK_DEPENDS
625 - elif ch == 'o' and self.check_keyword("n"):
624 + if ch == 'o' and self.check_keyword("n"):
625 return TOK_ON
627 - elif ch == 's' and self.check_keyword("elect"):
626 + if ch == 's' and self.check_keyword("elect"):
627 return TOK_SELECT
629 - elif ch == 'i' and self.check_keyword("mply"):
628 + if ch == 'i' and self.check_keyword("mply"):
629 return TOK_IMPLY
631 - elif ch == 'c' and self.check_keyword("onfig"):
630 + if ch == 'c' and self.check_keyword("onfig"):
631 return TOK_CONFIG
633 - elif ch == 'd' and self.check_keyword("efault"):
632 + if ch == 'd' and self.check_keyword("efault"):
633 return TOK_DEFAULT
635 - elif ch == 'b' and self.check_keyword("ool"):
634 + if ch == 'b' and self.check_keyword("ool"):
635 return TOK_BOOL
637 - elif ch == 'i' and self.check_keyword("f"):
636 + if ch == 'i' and self.check_keyword("f"):
637 return TOK_IF
639 - elif ch == 'y' and self.check_keyword(""):
638 + if ch == 'y' and self.check_keyword(""):
639 return TOK_Y
641 - elif ch == 'n' and self.check_keyword(""):
640 + if ch == 'n' and self.check_keyword(""):
641 return TOK_N
643 - elif (ch == 's' and self.check_keyword("ource")) or \
642 + if (ch == 's' and self.check_keyword("ource")) or \
643 ch == 'i' and self.check_keyword("nclude"):
644 # source FILENAME
645 # include FILENAME
@@ -650,21 +649,23 @@ class KconfigParser:
649 self.cursor = self.src.find('\n', self.cursor)
650 self.val = self.src[start:self.cursor]
651 return TOK_SOURCE
653 - elif ch.isalnum():
652 + if ch.isalnum():
653 # identifier
654 while self.src[self.cursor].isalnum() or self.src[self.cursor] == '_':
655 self.cursor += 1
656 self.val = self.src[self.pos:self.cursor]
657 return TOK_ID
659 - elif ch == '\n':
658 + if ch == '\n':
659 if self.cursor == len(self.src):
660 return TOK_EOF
661 self.line += 1
662 self.line_pos = self.cursor
664 - elif not ch.isspace():
665 - raise KconfigParserError(self, 'invalid input', ch)
663 + return None
664 + if ch.isspace():
665 + return None
666 +
667 + raise KconfigParserError(self, 'invalid input', ch)
668
667 - return None
669
670 def main() -> None:
671 argv = sys.argv