Skip to content

Commit

Permalink
Rename SemanticModel::is_builtin to `SemanticModel::has_builtin_bin…
Browse files Browse the repository at this point in the history
…ding` (#10991)
  • Loading branch information
AlexWaygood committed Apr 18, 2024
1 parent 2cc487e commit e09180b
Show file tree
Hide file tree
Showing 46 changed files with 74 additions and 67 deletions.
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/checkers/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1909,7 +1909,7 @@ impl<'a> Checker<'a> {
}
{
let (all_names, all_flags) =
extract_all_names(parent, |name| self.semantic.is_builtin(name));
extract_all_names(parent, |name| self.semantic.has_builtin_binding(name));

if all_flags.intersects(DunderAllFlags::INVALID_OBJECT) {
flags |= BindingFlags::INVALID_ALL_OBJECT;
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/importer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl<'a> Importer<'a> {
at: TextSize,
semantic: &SemanticModel,
) -> Result<(Option<Edit>, String), ResolutionError> {
if semantic.is_builtin(symbol) {
if semantic.has_builtin_binding(symbol) {
return Ok((None, symbol.to_string()));
}
let (import_edit, binding) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ fn find_shell_keyword<'a>(
semantic: &SemanticModel,
) -> Option<ShellKeyword<'a>> {
arguments.find_keyword("shell").map(|keyword| ShellKeyword {
truthiness: Truthiness::from_expr(&keyword.value, |id| semantic.is_builtin(id)),
truthiness: Truthiness::from_expr(&keyword.value, |id| semantic.has_builtin_binding(id)),
keyword,
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub(crate) fn boolean_type_hint_positional_argument(
}

// If `bool` isn't actually a reference to the `bool` built-in, return.
if !checker.semantic().is_builtin("bool") {
if !checker.semantic().has_builtin_binding("bool") {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub(crate) fn unused_loop_control_variable(checker: &mut Checker, stmt_for: &ast
// Avoid fixing any variables that _may_ be used, but undetectably so.
let certainty =
Certainty::from(!helpers::uses_magic_variable_access(&stmt_for.body, |id| {
checker.semantic().is_builtin(id)
checker.semantic().has_builtin_binding(id)
}));

// Attempt to rename the variable by prepending an underscore, but avoid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub(crate) fn useless_expression(checker: &mut Checker, value: &Expr) {
}

// Ignore statements that have side effects.
if contains_effect(value, |id| checker.semantic().is_builtin(id)) {
if contains_effect(value, |id| checker.semantic().has_builtin_binding(id)) {
// Flag attributes as useless expressions, even if they're attached to calls or other
// expressions.
if value.is_attribute_expr() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn add_diagnostic(checker: &mut Checker, expr: &Expr) {
Expr::DictComp(_) => "dict",
_ => return,
};
if !checker.semantic().is_builtin(id) {
if !checker.semantic().has_builtin_binding(id) {
return;
}
let mut diagnostic = Diagnostic::new(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub(crate) fn unnecessary_generator_list(checker: &mut Checker, call: &ast::Expr
) else {
return;
};
if !checker.semantic().is_builtin("list") {
if !checker.semantic().has_builtin_binding("list") {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub(crate) fn unnecessary_generator_set(checker: &mut Checker, call: &ast::ExprC
) else {
return;
};
if !checker.semantic().is_builtin("set") {
if !checker.semantic().has_builtin_binding("set") {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub(crate) fn unnecessary_list_call(
let Some(argument) = helpers::first_argument_with_matching_function("list", func, args) else {
return;
};
if !checker.semantic().is_builtin("list") {
if !checker.semantic().has_builtin_binding("list") {
return;
}
if !argument.is_list_comp_expr() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub(crate) fn unnecessary_list_comprehension_dict(
else {
return;
};
if !checker.semantic().is_builtin("dict") {
if !checker.semantic().has_builtin_binding("dict") {
return;
}
let Expr::ListComp(ast::ExprListComp { elt, .. }) = argument else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub(crate) fn unnecessary_list_comprehension_set(checker: &mut Checker, call: &a
) else {
return;
};
if !checker.semantic().is_builtin("set") {
if !checker.semantic().has_builtin_binding("set") {
return;
}
if argument.is_list_comp_expr() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub(crate) fn unnecessary_literal_dict(
else {
return;
};
if !checker.semantic().is_builtin("dict") {
if !checker.semantic().has_builtin_binding("dict") {
return;
}
let (kind, elts) = match argument {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub(crate) fn unnecessary_literal_set(checker: &mut Checker, call: &ast::ExprCal
) else {
return;
};
if !checker.semantic().is_builtin("set") {
if !checker.semantic().has_builtin_binding("set") {
return;
}
let kind = match argument {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub(crate) fn unnecessary_literal_within_dict_call(checker: &mut Checker, call:
else {
return;
};
if !checker.semantic().is_builtin("dict") {
if !checker.semantic().has_builtin_binding("dict") {
return;
}
let argument_kind = match argument {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub(crate) fn unnecessary_literal_within_list_call(checker: &mut Checker, call:
else {
return;
};
if !checker.semantic().is_builtin("list") {
if !checker.semantic().has_builtin_binding("list") {
return;
}
let argument_kind = match argument {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub(crate) fn unnecessary_literal_within_tuple_call(checker: &mut Checker, call:
) else {
return;
};
if !checker.semantic().is_builtin("tuple") {
if !checker.semantic().has_builtin_binding("tuple") {
return;
}
let argument_kind = match argument {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ fn exc_info_arg_is_falsey(call: &ExprCall, checker: &mut Checker) -> bool {
.find_keyword("exc_info")
.map(|keyword| &keyword.value)
.is_some_and(|value| {
let truthiness = Truthiness::from_expr(value, |id| checker.semantic().is_builtin(id));
let truthiness =
Truthiness::from_expr(value, |id| checker.semantic().has_builtin_binding(id));
matches!(truthiness, Truthiness::False | Truthiness::Falsey)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub(crate) fn invalid_get_logger_argument(checker: &mut Checker, call: &ast::Exp
return;
}

if !checker.semantic().is_builtin(id) {
if !checker.semantic().has_builtin_binding(id) {
return;
}

Expand All @@ -84,7 +84,7 @@ pub(crate) fn invalid_get_logger_argument(checker: &mut Checker, call: &ast::Exp
}

let mut diagnostic = Diagnostic::new(InvalidGetLoggerArgument, expr.range());
if checker.semantic().is_builtin("__name__") {
if checker.semantic().has_builtin_binding("__name__") {
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
"__name__".to_string(),
expr.range(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub(crate) fn unnecessary_type_union<'a>(checker: &mut Checker, union: &'a Expr)
union.range(),
);

if semantic.is_builtin("type") {
if semantic.has_builtin_binding("type") {
let content = if let Some(subscript) = subscript {
let types = &Expr::Subscript(ast::ExprSubscript {
value: Box::new(Expr::Name(ast::ExprName {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ fn to_pytest_raises_args<'a>(

/// PT015
pub(crate) fn assert_falsy(checker: &mut Checker, stmt: &Stmt, test: &Expr) {
let truthiness = Truthiness::from_expr(test, |id| checker.semantic().is_builtin(id));
let truthiness = Truthiness::from_expr(test, |id| checker.semantic().has_builtin_binding(id));
if matches!(truthiness, Truthiness::False | Truthiness::Falsey) {
checker
.diagnostics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ pub(crate) fn duplicate_isinstance_call(checker: &mut Checker, expr: &Expr) {
},
expr.range(),
);
if !contains_effect(target, |id| checker.semantic().is_builtin(id)) {
if !contains_effect(target, |id| checker.semantic().has_builtin_binding(id)) {
// Grab the types used in each duplicate `isinstance` call (e.g., `int` and `str`
// in `isinstance(obj, int) or isinstance(obj, str)`).
let types: Vec<&Expr> = indices
Expand Down Expand Up @@ -520,7 +520,7 @@ pub(crate) fn compare_with_tuple(checker: &mut Checker, expr: &Expr) {
// Avoid rewriting (e.g.) `a == "foo" or a == f()`.
if comparators
.iter()
.any(|expr| contains_effect(expr, |id| checker.semantic().is_builtin(id)))
.any(|expr| contains_effect(expr, |id| checker.semantic().has_builtin_binding(id)))
{
continue;
}
Expand Down Expand Up @@ -614,7 +614,7 @@ pub(crate) fn expr_and_not_expr(checker: &mut Checker, expr: &Expr) {
return;
}

if contains_effect(expr, |id| checker.semantic().is_builtin(id)) {
if contains_effect(expr, |id| checker.semantic().has_builtin_binding(id)) {
return;
}

Expand Down Expand Up @@ -671,7 +671,7 @@ pub(crate) fn expr_or_not_expr(checker: &mut Checker, expr: &Expr) {
return;
}

if contains_effect(expr, |id| checker.semantic().is_builtin(id)) {
if contains_effect(expr, |id| checker.semantic().has_builtin_binding(id)) {
return;
}

Expand Down Expand Up @@ -748,14 +748,15 @@ fn is_short_circuit(

for (index, (value, next_value)) in values.iter().tuple_windows().enumerate() {
// Keep track of the location of the furthest-right, truthy or falsey expression.
let value_truthiness = Truthiness::from_expr(value, |id| checker.semantic().is_builtin(id));
let value_truthiness =
Truthiness::from_expr(value, |id| checker.semantic().has_builtin_binding(id));
let next_value_truthiness =
Truthiness::from_expr(next_value, |id| checker.semantic().is_builtin(id));
Truthiness::from_expr(next_value, |id| checker.semantic().has_builtin_binding(id));

// Keep track of the location of the furthest-right, non-effectful expression.
if value_truthiness.is_unknown()
&& (!checker.semantic().in_boolean_test()
|| contains_effect(value, |id| checker.semantic().is_builtin(id)))
|| contains_effect(value, |id| checker.semantic().has_builtin_binding(id)))
{
furthest = next_value;
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub(crate) fn if_expr_with_true_false(
.to_string(),
expr.range(),
)));
} else if checker.semantic().is_builtin("bool") {
} else if checker.semantic().has_builtin_binding("bool") {
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
checker.generator().expr(
&ast::ExprCall {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ pub(crate) fn double_negation(checker: &mut Checker, expr: &Expr, op: UnaryOp, o
checker.locator().slice(operand.as_ref()).to_string(),
expr.range(),
)));
} else if checker.semantic().is_builtin("bool") {
} else if checker.semantic().has_builtin_binding("bool") {
let node = ast::ExprName {
id: "bool".into(),
ctx: ExprContext::Load,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ pub(crate) fn if_else_block_instead_of_dict_get(checker: &mut Checker, stmt_if:
}

// Check that the default value is not "complex".
if contains_effect(default_value, |id| checker.semantic().is_builtin(id)) {
if contains_effect(default_value, |id| {
checker.semantic().has_builtin_binding(id)
}) {
return;
}

Expand Down Expand Up @@ -261,7 +263,9 @@ pub(crate) fn if_exp_instead_of_dict_get(
}

// Check that the default value is not "complex".
if contains_effect(default_value, |id| checker.semantic().is_builtin(id)) {
if contains_effect(default_value, |id| {
checker.semantic().has_builtin_binding(id)
}) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,9 @@ pub(crate) fn if_else_block_instead_of_dict_lookup(checker: &mut Checker, stmt_i
return;
};

if value
.as_ref()
.is_some_and(|value| contains_effect(value, |id| checker.semantic().is_builtin(id)))
{
if value.as_ref().is_some_and(|value| {
contains_effect(value, |id| checker.semantic().has_builtin_binding(id))
}) {
return;
}

Expand Down Expand Up @@ -112,7 +111,7 @@ pub(crate) fn if_else_block_instead_of_dict_lookup(checker: &mut Checker, stmt_i
return;
};
if value.as_ref().is_some_and(|value| {
contains_effect(value, |id| checker.semantic().is_builtin(id))
contains_effect(value, |id| checker.semantic().has_builtin_binding(id))
}) {
return;
};
Expand All @@ -138,7 +137,7 @@ pub(crate) fn if_else_block_instead_of_dict_lookup(checker: &mut Checker, stmt_i
};

if value.as_ref().is_some_and(|value| {
contains_effect(value, |id| checker.semantic().is_builtin(id))
contains_effect(value, |id| checker.semantic().has_builtin_binding(id))
}) {
return;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub(crate) fn needless_bool(checker: &mut Checker, stmt: &Stmt) {
// If the condition is a comparison, we can replace it with the condition, since we
// know it's a boolean.
Some(if_test.clone())
} else if checker.semantic().is_builtin("bool") {
} else if checker.semantic().has_builtin_binding("bool") {
// Otherwise, we need to wrap the condition in a call to `bool`.
let func_node = ast::ExprName {
id: "bool".into(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub(crate) fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt) {
},
TextRange::new(stmt.start(), terminal.stmt.end()),
);
if checker.semantic().is_builtin("any") {
if checker.semantic().has_builtin_binding("any") {
diagnostic.set_fix(Fix::unsafe_edit(Edit::replacement(
contents,
stmt.start(),
Expand Down Expand Up @@ -199,7 +199,7 @@ pub(crate) fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt) {
},
TextRange::new(stmt.start(), terminal.stmt.end()),
);
if checker.semantic().is_builtin("all") {
if checker.semantic().has_builtin_binding("all") {
diagnostic.set_fix(Fix::unsafe_edit(Edit::replacement(
contents,
stmt.start(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ fn deprecated_type_comparison(checker: &mut Checker, compare: &ast::ExprCompare)
| "dict"
| "set"
| "memoryview"
) && semantic.is_builtin(id)
) && semantic.has_builtin_binding(id)
{
checker.diagnostics.push(Diagnostic::new(
TypeComparison {
Expand Down Expand Up @@ -289,7 +289,7 @@ fn is_type(expr: &Expr, semantic: &SemanticModel) -> bool {
| "ValueError"
| "Warning"
| "ZeroDivisionError"
) && semantic.is_builtin(id)
) && semantic.has_builtin_binding(id)
}
_ => false,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ fn remove_unused_variable(binding: &Binding, checker: &Checker) -> Option<Fix> {
{
if target.is_name_expr() {
return if targets.len() > 1
|| contains_effect(value, |id| checker.semantic().is_builtin(id))
|| contains_effect(value, |id| checker.semantic().has_builtin_binding(id))
{
// If the expression is complex (`x = foo()`), remove the assignment,
// but preserve the right-hand side.
Expand Down Expand Up @@ -265,7 +265,7 @@ fn remove_unused_variable(binding: &Binding, checker: &Checker) -> Option<Fix> {
}) = statement
{
if target.is_name_expr() {
return if contains_effect(value, |id| checker.semantic().is_builtin(id)) {
return if contains_effect(value, |id| checker.semantic().has_builtin_binding(id)) {
// If the expression is complex (`x = foo()`), remove the assignment,
// but preserve the right-hand side.
let start = statement.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ pub(crate) fn if_stmt_min_max(checker: &mut Checker, stmt_if: &ast::StmtIf) {
stmt_if.range(),
);

if checker.semantic().is_builtin(min_max.as_str()) {
if checker.semantic().has_builtin_binding(min_max.as_str()) {
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
replacement,
stmt_if.range(),
Expand Down
Loading

0 comments on commit e09180b

Please sign in to comment.