Merge "Fix a crash when a brand new file is added."
diff --git a/tools/checkstyle/checkstyle.py b/tools/checkstyle/checkstyle.py
index afb2b3e..f7518ed 100755
--- a/tools/checkstyle/checkstyle.py
+++ b/tools/checkstyle/checkstyle.py
@@ -106,9 +106,9 @@
     file_name = os.path.relpath(file_name)
     errors = file_element.getElementsByTagName('error')
     for error in errors:
-      line = error.attributes['line'].value
-      if last_commit_modified_files and int(line) not in modified_lines:
-        if error.attributes['source'].value not in FORCED_RULES:
+      line = int(error.attributes['line'].value)
+      rule = error.attributes['source'].value
+      if last_commit_modified_files and _ShouldSkip(modified_lines, line, rule):
           continue
 
       column = ''
@@ -125,6 +125,12 @@
   return (result_errors, result_warnings)
 
 
+# Returns whether an error on a given line should be skipped
+# based on the modified_lines list and the rule.
+def _ShouldSkip(modified_lines, line, rule):
+  return modified_lines and line not in modified_lines and rule not in FORCED_RULES
+
+
 def _GetModifiedFiles():
   root = git.repository_root()
   sha = git.last_commit()