Remove trailing punctuation from error messages (#983)

This commit is contained in:
Charlie Marsh 2022-12-01 12:25:37 -05:00 committed by GitHub
parent 6fe478cb43
commit 9e5df82578
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 65 additions and 74 deletions

View file

@ -46,7 +46,7 @@ fn main() -> Result<()> {
// Extract the Flake8 section.
let flake8 = config
.get("flake8")
.expect("Unable to find flake8 section in INI file.");
.expect("Unable to find flake8 section in INI file");
// Create the pyproject.toml.
let pyproject = converter::convert(flake8, cli.plugin)?;

View file

@ -73,13 +73,13 @@ pub fn main(cli: &Cli) -> Result<()> {
// Extract the prefix.
let index = existing
.find(BEGIN_PRAGMA)
.expect("Unable to find begin pragma.");
.expect("Unable to find begin pragma");
let prefix = &existing[..index + BEGIN_PRAGMA.len()];
// Extract the suffix.
let index = existing
.find(END_PRAGMA)
.expect("Unable to find end pragma.");
.expect("Unable to find end pragma");
let suffix = &existing[index..];
// Write the prefix, new contents, and suffix.

View file

@ -21,7 +21,7 @@ fn is_lone_child(child: &Stmt, parent: &Stmt, deleted: &[&Stmt]) -> Result<bool>
if body.iter().contains(child) {
Ok(has_single_child(body, deleted))
} else {
Err(anyhow::anyhow!("Unable to find child in parent body."))
Err(anyhow::anyhow!("Unable to find child in parent body"))
}
}
StmtKind::For { body, orelse, .. }
@ -33,7 +33,7 @@ fn is_lone_child(child: &Stmt, parent: &Stmt, deleted: &[&Stmt]) -> Result<bool>
} else if orelse.iter().contains(child) {
Ok(has_single_child(orelse, deleted))
} else {
Err(anyhow::anyhow!("Unable to find child in parent body."))
Err(anyhow::anyhow!("Unable to find child in parent body"))
}
}
StmtKind::Try {
@ -59,10 +59,10 @@ fn is_lone_child(child: &Stmt, parent: &Stmt, deleted: &[&Stmt]) -> Result<bool>
}) {
Ok(has_single_child(body, deleted))
} else {
Err(anyhow::anyhow!("Unable to find child in parent body."))
Err(anyhow::anyhow!("Unable to find child in parent body"))
}
}
_ => Err(anyhow::anyhow!("Unable to find child in parent body.")),
_ => Err(anyhow::anyhow!("Unable to find child in parent body")),
}
}

View file

@ -232,7 +232,7 @@ where
// Pre-visit.
match &stmt.node {
StmtKind::Global { names } => {
let scope_index = *self.scope_stack.last().expect("No current scope found.");
let scope_index = *self.scope_stack.last().expect("No current scope found");
if scope_index != GLOBAL_SCOPE_INDEX {
let scope = &mut self.scopes[scope_index];
let usage = Some((scope.id, Range::from_located(stmt)));
@ -272,7 +272,7 @@ where
}
}
StmtKind::Nonlocal { names } => {
let scope_index = *self.scope_stack.last().expect("No current scope found.");
let scope_index = *self.scope_stack.last().expect("No current scope found");
if scope_index != GLOBAL_SCOPE_INDEX {
let scope = &mut self.scopes[scope_index];
let usage = Some((scope.id, Range::from_located(stmt)));
@ -625,7 +625,7 @@ where
self.scopes[*(self
.scope_stack
.last()
.expect("No current scope found."))]
.expect("No current scope found"))]
.id,
Range::from_located(alias),
))
@ -750,7 +750,7 @@ where
self.scopes[*(self
.scope_stack
.last()
.expect("No current scope found."))]
.expect("No current scope found"))]
.id,
Range::from_located(alias),
)),
@ -790,7 +790,7 @@ where
if self.settings.enabled.contains(&CheckCode::F406) {
let scope = &self.scopes
[*(self.scope_stack.last().expect("No current scope found."))];
[*(self.scope_stack.last().expect("No current scope found"))];
if !matches!(scope.kind, ScopeKind::Module) {
self.add_check(Check::new(
CheckKind::ImportStarNotPermitted(helpers::format_import_from(
@ -813,7 +813,7 @@ where
}
let scope = &mut self.scopes
[*(self.scope_stack.last().expect("No current scope found."))];
[*(self.scope_stack.last().expect("No current scope found"))];
scope.import_starred = true;
} else {
if let Some(asname) = &alias.node.asname {
@ -849,7 +849,7 @@ where
self.scopes[*(self
.scope_stack
.last()
.expect("No current scope found."))]
.expect("No current scope found"))]
.id,
range,
))
@ -1657,7 +1657,7 @@ where
if let ExprKind::Name { id, ctx } = &func.node {
if id == "locals" && matches!(ctx, ExprContext::Load) {
let scope = &mut self.scopes
[*(self.scope_stack.last().expect("No current scope found."))];
[*(self.scope_stack.last().expect("No current scope found"))];
if let ScopeKind::Function(inner) = &mut scope.kind {
inner.uses_locals = true;
}
@ -2276,7 +2276,7 @@ where
if let Some(binding) = {
let scope = &mut self.scopes
[*(self.scope_stack.last().expect("No current scope found."))];
[*(self.scope_stack.last().expect("No current scope found"))];
&scope.values.remove(&name.as_str())
} {
if binding.used.is_none() {
@ -2291,7 +2291,7 @@ where
if let Some(binding) = definition {
let scope = &mut self.scopes
[*(self.scope_stack.last().expect("No current scope found."))];
[*(self.scope_stack.last().expect("No current scope found"))];
scope.values.insert(name, binding);
}
}
@ -2433,7 +2433,7 @@ impl<'a> Checker<'a> {
fn pop_parent(&mut self) {
self.parent_stack
.pop()
.expect("Attempted to pop without scope.");
.expect("Attempted to pop without scope");
}
fn push_scope(&mut self, scope: Scope<'a>) {
@ -2445,12 +2445,12 @@ impl<'a> Checker<'a> {
self.dead_scopes.push(
self.scope_stack
.pop()
.expect("Attempted to pop without scope."),
.expect("Attempted to pop without scope"),
);
}
fn bind_builtins(&mut self) {
let scope = &mut self.scopes[*(self.scope_stack.last().expect("No current scope found."))];
let scope = &mut self.scopes[*(self.scope_stack.last().expect("No current scope found"))];
for builtin in BUILTINS {
scope.values.insert(
@ -2475,7 +2475,7 @@ impl<'a> Checker<'a> {
}
pub fn current_scope(&self) -> &Scope {
&self.scopes[*(self.scope_stack.last().expect("No current scope found."))]
&self.scopes[*(self.scope_stack.last().expect("No current scope found"))]
}
pub fn current_scopes(&self) -> impl Iterator<Item = &Scope> {
@ -2483,12 +2483,12 @@ impl<'a> Checker<'a> {
}
pub fn current_parent(&self) -> &'a Stmt {
self.parents[*(self.parent_stack.last().expect("No parent found."))]
self.parents[*(self.parent_stack.last().expect("No parent found"))]
}
pub fn binding_context(&self) -> BindingContext {
let mut rev = self.parent_stack.iter().rev().fuse();
let defined_by = *rev.next().expect("Expected to bind within a statement.");
let defined_by = *rev.next().expect("Expected to bind within a statement");
let defined_in = rev.next().copied();
BindingContext {
defined_by,
@ -2536,7 +2536,7 @@ impl<'a> Checker<'a> {
},
};
let scope = &mut self.scopes[*(self.scope_stack.last().expect("No current scope found."))];
let scope = &mut self.scopes[*(self.scope_stack.last().expect("No current scope found"))];
scope.values.insert(name, binding);
}
@ -2743,7 +2743,7 @@ impl<'a> Checker<'a> {
}
let scope =
&mut self.scopes[*(self.scope_stack.last().expect("No current scope found."))];
&mut self.scopes[*(self.scope_stack.last().expect("No current scope found"))];
if scope.values.remove(&id.as_str()).is_none()
&& self.settings.enabled.contains(&CheckCode::F821)
{
@ -2839,7 +2839,7 @@ impl<'a> Checker<'a> {
}
self.deferred_assignments
.push(*self.scope_stack.last().expect("No current scope found."));
.push(*self.scope_stack.last().expect("No current scope found"));
self.pop_scope();
}
@ -2857,7 +2857,7 @@ impl<'a> Checker<'a> {
}
self.deferred_assignments
.push(*self.scope_stack.last().expect("No current scope found."));
.push(*self.scope_stack.last().expect("No current scope found"));
self.pop_scope();
}

View file

@ -4,7 +4,7 @@ use libcst_native::{Expr, Module, SmallStatement, Statement};
pub fn match_module(module_text: &str) -> Result<Module> {
match libcst_native::parse_module(module_text, None) {
Ok(module) => Ok(module),
Err(_) => Err(anyhow::anyhow!("Failed to extract CST from source.")),
Err(_) => Err(anyhow::anyhow!("Failed to extract CST from source")),
}
}
@ -13,9 +13,9 @@ pub fn match_expr<'a, 'b>(module: &'a mut Module<'b>) -> Result<&'a mut Expr<'b>
if let Some(SmallStatement::Expr(expr)) = expr.body.first_mut() {
Ok(expr)
} else {
Err(anyhow::anyhow!("Expected node to be: SmallStatement::Expr"))
Err(anyhow::anyhow!("Expected SmallStatement::Expr"))
}
} else {
Err(anyhow::anyhow!("Expected node to be: Statement::Simple"))
Err(anyhow::anyhow!("Expected Statement::Simple"))
}
}

View file

@ -45,9 +45,7 @@ fn match_tuple_range<T>(located: &Located<T>, locator: &SourceCodeLocator) -> Re
end_location,
})
} else {
Err(anyhow::anyhow!(
"Unable to find left and right parentheses."
))
Err(anyhow::anyhow!("Unable to find left and right parentheses"))
}
}

View file

@ -15,7 +15,7 @@ fn match_call<'a, 'b>(expr: &'a mut Expr<'b>) -> Result<&'a mut Call<'b>> {
if let Expression::Call(call) = &mut expr.value {
Ok(call)
} else {
Err(anyhow::anyhow!("Expected node to be: Expression::Call"))
Err(anyhow::anyhow!("Expected Expression::Call"))
}
}
@ -23,7 +23,7 @@ fn match_arg<'a, 'b>(call: &'a Call<'b>) -> Result<&'a Arg<'b>> {
if let Some(arg) = call.args.first() {
Ok(arg)
} else {
Err(anyhow::anyhow!("Expected node to be: Arg"))
Err(anyhow::anyhow!("Expected Arg"))
}
}
@ -41,7 +41,7 @@ pub fn fix_unnecessary_generator_list(
let Expression::GeneratorExp(generator_exp) = &arg.value else {
return Err(anyhow::anyhow!(
"Expected node to be: Expression::GeneratorExp"
"Expected Expression::GeneratorExp"
));
};
@ -82,7 +82,7 @@ pub fn fix_unnecessary_generator_set(
let Expression::GeneratorExp(generator_exp) = &arg.value else {
return Err(anyhow::anyhow!(
"Expected node to be: Expression::GeneratorExp"
"Expected Expression::GeneratorExp"
));
};
@ -124,11 +124,11 @@ pub fn fix_unnecessary_generator_dict(
// Extract the (k, v) from `(k, v) for ...`.
let Expression::GeneratorExp(generator_exp) = &arg.value else {
return Err(anyhow::anyhow!(
"Expected node to be: Expression::GeneratorExp"
"Expected Expression::GeneratorExp"
));
};
let Expression::Tuple(tuple) = &generator_exp.elt.as_ref() else {
return Err(anyhow::anyhow!("Expected node to be: Expression::Tuple"));
return Err(anyhow::anyhow!("Expected Expression::Tuple"));
};
let Some(Element::Simple { value: key, .. }) = &tuple.elements.get(0) else {
return Err(anyhow::anyhow!(
@ -181,7 +181,7 @@ pub fn fix_unnecessary_list_comprehension_set(
let arg = match_arg(call)?;
let Expression::ListComp(list_comp) = &arg.value else {
return Err(anyhow::anyhow!("Expected node to be: Expression::ListComp"));
return Err(anyhow::anyhow!("Expected Expression::ListComp"));
};
body.value = Expression::SetComp(Box::new(SetComp {
@ -220,11 +220,11 @@ pub fn fix_unnecessary_list_comprehension_dict(
let arg = match_arg(call)?;
let Expression::ListComp(list_comp) = &arg.value else {
return Err(anyhow::anyhow!("Expected node to be: Expression::ListComp"));
return Err(anyhow::anyhow!("Expected Expression::ListComp"));
};
let Expression::Tuple(tuple) = &*list_comp.elt else {
return Err(anyhow::anyhow!("Expected node to be: Expression::Tuple"));
return Err(anyhow::anyhow!("Expected Expression::Tuple"));
};
let [Element::Simple {
@ -275,7 +275,7 @@ pub fn fix_unnecessary_literal_set(
Expression::List(inner) => &inner.elements,
_ => {
return Err(anyhow::anyhow!(
"Expected node to be: Expression::Tuple | Expression::List"
"Expected Expression::Tuple | Expression::List"
))
}
};
@ -323,7 +323,7 @@ pub fn fix_unnecessary_literal_dict(
Expression::List(inner) => &inner.elements,
_ => {
return Err(anyhow::anyhow!(
"Expected node to be: Expression::Tuple | Expression::List"
"Expected Expression::Tuple | Expression::List"
))
}
};
@ -389,7 +389,7 @@ pub fn fix_unnecessary_collection_call(
let mut body = match_expr(&mut tree)?;
let call = match_call(body)?;
let Expression::Name(name) = &call.func.as_ref() else {
return Err(anyhow::anyhow!("Expected node to be: Expression::Name"));
return Err(anyhow::anyhow!("Expected Expression::Name"));
};
// Arena allocator used to create formatted strings of sufficient lifetime,
@ -520,7 +520,7 @@ pub fn fix_unnecessary_literal_within_tuple_call(
),
_ => {
return Err(anyhow::anyhow!(
"Expected node to be: Expression::Tuple | Expression::List"
"Expected Expression::Tuple | Expression::List"
))
}
};
@ -576,7 +576,7 @@ pub fn fix_unnecessary_literal_within_list_call(
),
_ => {
return Err(anyhow::anyhow!(
"Expected node to be: Expression::Tuple | Expression::List"
"Expected Expression::Tuple | Expression::List"
))
}
};
@ -643,7 +643,7 @@ pub fn fix_unnecessary_call_around_sorted(
if let Expression::Call(call) = &arg.value {
call
} else {
return Err(anyhow::anyhow!("Expected node to be: Expression::Call "));
return Err(anyhow::anyhow!("Expected Expression::Call "));
}
}
_ => {
@ -769,7 +769,7 @@ pub fn fix_unnecessary_comprehension(
}
_ => {
return Err(anyhow::anyhow!(
"Expected node to be: Expression::ListComp | Expression:SetComp"
"Expected Expression::ListComp | Expression:SetComp"
))
}
}

View file

@ -97,7 +97,7 @@ fn extract_quote(text: &str) -> &str {
}
}
panic!("Unable to find quotation mark for String token.")
panic!("Unable to find quotation mark for String token")
}
/// W605

View file

@ -900,7 +900,7 @@ fn blanks_and_section_underline(
) {
let docstring = definition
.docstring
.expect("Sections are only available for docstrings.");
.expect("Sections are only available for docstrings");
let mut blank_lines_after_header = 0;
for line in context.following_lines {
@ -1157,7 +1157,7 @@ fn common_section(
) {
let docstring = definition
.docstring
.expect("Sections are only available for docstrings.");
.expect("Sections are only available for docstrings");
if checker.settings.enabled.contains(&CheckCode::D405) {
if !style
@ -1439,7 +1439,7 @@ fn numpy_section(checker: &mut Checker, definition: &Definition, context: &Secti
if !suffix.is_empty() {
let docstring = definition
.docstring
.expect("Sections are only available for docstrings.");
.expect("Sections are only available for docstrings");
let mut check = Check::new(
CheckKind::NewLineAfterSectionName(context.section_name.to_string()),
Range::from_located(docstring),
@ -1488,7 +1488,7 @@ fn google_section(checker: &mut Checker, definition: &Definition, context: &Sect
if suffix != ":" {
let docstring = definition
.docstring
.expect("Sections are only available for docstrings.");
.expect("Sections are only available for docstrings");
let mut check = Check::new(
CheckKind::SectionNameEndsInColon(context.section_name.to_string()),
Range::from_located(docstring),

View file

@ -371,7 +371,7 @@ pub fn if_tuple(test: &Expr, location: Range) -> Option<Check> {
/// F821
pub fn undefined_local(scopes: &[&Scope], name: &str) -> Option<Check> {
let current = &scopes.last().expect("No current scope found.");
let current = &scopes.last().expect("No current scope found");
if matches!(current.kind, ScopeKind::Function(_)) && !current.values.contains_key(name) {
for scope in scopes.iter().rev().skip(1) {
if matches!(scope.kind, ScopeKind::Function(_) | ScopeKind::Module) {

View file

@ -23,7 +23,7 @@ pub fn remove_unused_imports(
let mut tree = match_module(&module_text)?;
let Some(Statement::Simple(body)) = tree.body.first_mut() else {
return Err(anyhow::anyhow!("Expected node to be: Statement::Simple"));
return Err(anyhow::anyhow!("Expected Statement::Simple"));
};
let (aliases, import_module) = match body.body.first_mut() {
@ -32,11 +32,11 @@ pub fn remove_unused_imports(
if let ImportNames::Aliases(names) = &mut import_body.names {
Ok((names, import_body.module.as_ref()))
} else {
Err(anyhow::anyhow!("Expected node to be: Aliases"))
Err(anyhow::anyhow!("Expected Aliases"))
}
}
_ => Err(anyhow::anyhow!(
"Expected node to be: SmallStatement::ImportFrom or SmallStatement::Import"
"Expected SmallStatement::ImportFrom or SmallStatement::Import"
)),
}?;
@ -83,9 +83,7 @@ fn match_comparison<'a, 'b>(expr: &'a mut Expr<'b>) -> Result<&'a mut Comparison
if let Expression::Comparison(comparison) = &mut expr.value {
Ok(comparison)
} else {
Err(anyhow::anyhow!(
"Expected node to be: Expression::Comparison"
))
Err(anyhow::anyhow!("Expected Expression::Comparison"))
}
}
@ -117,7 +115,7 @@ pub fn fix_invalid_literal_comparison(locator: &SourceCodeLocator, location: Ran
whitespace_after: a.clone(),
}),
op => Err(anyhow::anyhow!(
"Unexpected operator: {:?}. Expected Is or IsNot.",
"Unexpected operator: {:?} (expected CompOp::Is or CompOp::IsNot)",
op
)),
}?;

View file

@ -148,16 +148,16 @@ pub fn remove_unnecessary_future_import(
let mut tree = match_module(&module_text)?;
let Some(Statement::Simple(body)) = tree.body.first_mut() else {
return Err(anyhow::anyhow!("Expected node to be: Statement::Simple"));
return Err(anyhow::anyhow!("Expected Statement::Simple"));
};
let Some(SmallStatement::ImportFrom(body)) = body.body.first_mut() else {
return Err(anyhow::anyhow!(
"Expected node to be: SmallStatement::ImportFrom"
"Expected SmallStatement::ImportFrom"
));
};
let ImportNames::Aliases(aliases) = &mut body.names else {
return Err(anyhow::anyhow!("Expected node to be: Aliases"));
return Err(anyhow::anyhow!("Expected Aliases"));
};
// Preserve the trailing comma (or not) from the last entry.

View file

@ -141,7 +141,7 @@ fn create_remove_param_fix(
match (fix_start, fix_end) {
(Some(start), Some(end)) => Ok(Fix::deletion(start, end)),
_ => Err(anyhow::anyhow!(
"Failed to locate start and end parentheses."
"Failed to locate start and end parentheses"
)),
}
}

View file

@ -358,19 +358,14 @@ other-attribute = 1
fn find_and_parse_pyproject_toml() -> Result<()> {
let cwd = current_dir()?;
let project_root =
find_project_root(&[PathBuf::from("resources/test/fixtures/__init__.py")])
.expect("Unable to find project root.");
find_project_root(&[PathBuf::from("resources/test/fixtures/__init__.py")]).unwrap();
assert_eq!(project_root, cwd.join("resources/test/fixtures"));
let path =
find_pyproject_toml(Some(&project_root)).expect("Unable to find pyproject.toml.");
let path = find_pyproject_toml(Some(&project_root)).unwrap();
assert_eq!(path, cwd.join("resources/test/fixtures/pyproject.toml"));
let pyproject = parse_pyproject_toml(&path)?;
let config = pyproject
.tool
.and_then(|tool| tool.ruff)
.expect("Unable to find tool.ruff.");
let config = pyproject.tool.and_then(|tool| tool.ruff).unwrap();
assert_eq!(
config,
Options {