Skip to content

Commit e09180b

Browse files
authored
Rename SemanticModel::is_builtin to SemanticModel::has_builtin_binding (#10991)
1 parent 2cc487e commit e09180b

File tree

46 files changed

+74
-67
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+74
-67
lines changed

crates/ruff_linter/src/checkers/ast/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1909,7 +1909,7 @@ impl<'a> Checker<'a> {
19091909
}
19101910
{
19111911
let (all_names, all_flags) =
1912-
extract_all_names(parent, |name| self.semantic.is_builtin(name));
1912+
extract_all_names(parent, |name| self.semantic.has_builtin_binding(name));
19131913

19141914
if all_flags.intersects(DunderAllFlags::INVALID_OBJECT) {
19151915
flags |= BindingFlags::INVALID_ALL_OBJECT;

crates/ruff_linter/src/importer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl<'a> Importer<'a> {
246246
at: TextSize,
247247
semantic: &SemanticModel,
248248
) -> Result<(Option<Edit>, String), ResolutionError> {
249-
if semantic.is_builtin(symbol) {
249+
if semantic.has_builtin_binding(symbol) {
250250
return Ok((None, symbol.to_string()));
251251
}
252252
let (import_edit, binding) =

crates/ruff_linter/src/rules/flake8_bandit/rules/shell_injection.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ fn find_shell_keyword<'a>(
464464
semantic: &SemanticModel,
465465
) -> Option<ShellKeyword<'a>> {
466466
arguments.find_keyword("shell").map(|keyword| ShellKeyword {
467-
truthiness: Truthiness::from_expr(&keyword.value, |id| semantic.is_builtin(id)),
467+
truthiness: Truthiness::from_expr(&keyword.value, |id| semantic.has_builtin_binding(id)),
468468
keyword,
469469
})
470470
}

crates/ruff_linter/src/rules/flake8_boolean_trap/rules/boolean_type_hint_positional_argument.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub(crate) fn boolean_type_hint_positional_argument(
148148
}
149149

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

crates/ruff_linter/src/rules/flake8_bugbear/rules/unused_loop_control_variable.rs

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

113113
// Attempt to rename the variable by prepending an underscore, but avoid

crates/ruff_linter/src/rules/flake8_bugbear/rules/useless_expression.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub(crate) fn useless_expression(checker: &mut Checker, value: &Expr) {
9595
}
9696

9797
// Ignore statements that have side effects.
98-
if contains_effect(value, |id| checker.semantic().is_builtin(id)) {
98+
if contains_effect(value, |id| checker.semantic().has_builtin_binding(id)) {
9999
// Flag attributes as useless expressions, even if they're attached to calls or other
100100
// expressions.
101101
if value.is_attribute_expr() {

crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_comprehension.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ fn add_diagnostic(checker: &mut Checker, expr: &Expr) {
6060
Expr::DictComp(_) => "dict",
6161
_ => return,
6262
};
63-
if !checker.semantic().is_builtin(id) {
63+
if !checker.semantic().has_builtin_binding(id) {
6464
return;
6565
}
6666
let mut diagnostic = Diagnostic::new(

crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub(crate) fn unnecessary_generator_list(checker: &mut Checker, call: &ast::Expr
7171
) else {
7272
return;
7373
};
74-
if !checker.semantic().is_builtin("list") {
74+
if !checker.semantic().has_builtin_binding("list") {
7575
return;
7676
}
7777

crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_generator_set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub(crate) fn unnecessary_generator_set(checker: &mut Checker, call: &ast::ExprC
7272
) else {
7373
return;
7474
};
75-
if !checker.semantic().is_builtin("set") {
75+
if !checker.semantic().has_builtin_binding("set") {
7676
return;
7777
}
7878

crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_list_call.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub(crate) fn unnecessary_list_call(
5353
let Some(argument) = helpers::first_argument_with_matching_function("list", func, args) else {
5454
return;
5555
};
56-
if !checker.semantic().is_builtin("list") {
56+
if !checker.semantic().has_builtin_binding("list") {
5757
return;
5858
}
5959
if !argument.is_list_comp_expr() {

crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_list_comprehension_dict.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub(crate) fn unnecessary_list_comprehension_dict(
5656
else {
5757
return;
5858
};
59-
if !checker.semantic().is_builtin("dict") {
59+
if !checker.semantic().has_builtin_binding("dict") {
6060
return;
6161
}
6262
let Expr::ListComp(ast::ExprListComp { elt, .. }) = argument else {

crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_list_comprehension_set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub(crate) fn unnecessary_list_comprehension_set(checker: &mut Checker, call: &a
5252
) else {
5353
return;
5454
};
55-
if !checker.semantic().is_builtin("set") {
55+
if !checker.semantic().has_builtin_binding("set") {
5656
return;
5757
}
5858
if argument.is_list_comp_expr() {

crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_literal_dict.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub(crate) fn unnecessary_literal_dict(
6363
else {
6464
return;
6565
};
66-
if !checker.semantic().is_builtin("dict") {
66+
if !checker.semantic().has_builtin_binding("dict") {
6767
return;
6868
}
6969
let (kind, elts) = match argument {

crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_literal_set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub(crate) fn unnecessary_literal_set(checker: &mut Checker, call: &ast::ExprCal
6060
) else {
6161
return;
6262
};
63-
if !checker.semantic().is_builtin("set") {
63+
if !checker.semantic().has_builtin_binding("set") {
6464
return;
6565
}
6666
let kind = match argument {

crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_literal_within_dict_call.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub(crate) fn unnecessary_literal_within_dict_call(checker: &mut Checker, call:
7575
else {
7676
return;
7777
};
78-
if !checker.semantic().is_builtin("dict") {
78+
if !checker.semantic().has_builtin_binding("dict") {
7979
return;
8080
}
8181
let argument_kind = match argument {

crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_literal_within_list_call.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub(crate) fn unnecessary_literal_within_list_call(checker: &mut Checker, call:
7777
else {
7878
return;
7979
};
80-
if !checker.semantic().is_builtin("list") {
80+
if !checker.semantic().has_builtin_binding("list") {
8181
return;
8282
}
8383
let argument_kind = match argument {

crates/ruff_linter/src/rules/flake8_comprehensions/rules/unnecessary_literal_within_tuple_call.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub(crate) fn unnecessary_literal_within_tuple_call(checker: &mut Checker, call:
8080
) else {
8181
return;
8282
};
83-
if !checker.semantic().is_builtin("tuple") {
83+
if !checker.semantic().has_builtin_binding("tuple") {
8484
return;
8585
}
8686
let argument_kind = match argument {

crates/ruff_linter/src/rules/flake8_logging/rules/exception_without_exc_info.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ fn exc_info_arg_is_falsey(call: &ExprCall, checker: &mut Checker) -> bool {
8585
.find_keyword("exc_info")
8686
.map(|keyword| &keyword.value)
8787
.is_some_and(|value| {
88-
let truthiness = Truthiness::from_expr(value, |id| checker.semantic().is_builtin(id));
88+
let truthiness =
89+
Truthiness::from_expr(value, |id| checker.semantic().has_builtin_binding(id));
8990
matches!(truthiness, Truthiness::False | Truthiness::Falsey)
9091
})
9192
}

crates/ruff_linter/src/rules/flake8_logging/rules/invalid_get_logger_argument.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub(crate) fn invalid_get_logger_argument(checker: &mut Checker, call: &ast::Exp
7171
return;
7272
}
7373

74-
if !checker.semantic().is_builtin(id) {
74+
if !checker.semantic().has_builtin_binding(id) {
7575
return;
7676
}
7777

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

8686
let mut diagnostic = Diagnostic::new(InvalidGetLoggerArgument, expr.range());
87-
if checker.semantic().is_builtin("__name__") {
87+
if checker.semantic().has_builtin_binding("__name__") {
8888
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
8989
"__name__".to_string(),
9090
expr.range(),

crates/ruff_linter/src/rules/flake8_pyi/rules/unnecessary_type_union.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub(crate) fn unnecessary_type_union<'a>(checker: &mut Checker, union: &'a Expr)
9797
union.range(),
9898
);
9999

100-
if semantic.is_builtin("type") {
100+
if semantic.has_builtin_binding("type") {
101101
let content = if let Some(subscript) = subscript {
102102
let types = &Expr::Subscript(ast::ExprSubscript {
103103
value: Box::new(Expr::Name(ast::ExprName {

crates/ruff_linter/src/rules/flake8_pytest_style/rules/assertion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ fn to_pytest_raises_args<'a>(
488488

489489
/// PT015
490490
pub(crate) fn assert_falsy(checker: &mut Checker, stmt: &Stmt, test: &Expr) {
491-
let truthiness = Truthiness::from_expr(test, |id| checker.semantic().is_builtin(id));
491+
let truthiness = Truthiness::from_expr(test, |id| checker.semantic().has_builtin_binding(id));
492492
if matches!(truthiness, Truthiness::False | Truthiness::Falsey) {
493493
checker
494494
.diagnostics

crates/ruff_linter/src/rules/flake8_simplify/rules/ast_bool_op.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ pub(crate) fn duplicate_isinstance_call(checker: &mut Checker, expr: &Expr) {
385385
},
386386
expr.range(),
387387
);
388-
if !contains_effect(target, |id| checker.semantic().is_builtin(id)) {
388+
if !contains_effect(target, |id| checker.semantic().has_builtin_binding(id)) {
389389
// Grab the types used in each duplicate `isinstance` call (e.g., `int` and `str`
390390
// in `isinstance(obj, int) or isinstance(obj, str)`).
391391
let types: Vec<&Expr> = indices
@@ -520,7 +520,7 @@ pub(crate) fn compare_with_tuple(checker: &mut Checker, expr: &Expr) {
520520
// Avoid rewriting (e.g.) `a == "foo" or a == f()`.
521521
if comparators
522522
.iter()
523-
.any(|expr| contains_effect(expr, |id| checker.semantic().is_builtin(id)))
523+
.any(|expr| contains_effect(expr, |id| checker.semantic().has_builtin_binding(id)))
524524
{
525525
continue;
526526
}
@@ -614,7 +614,7 @@ pub(crate) fn expr_and_not_expr(checker: &mut Checker, expr: &Expr) {
614614
return;
615615
}
616616

617-
if contains_effect(expr, |id| checker.semantic().is_builtin(id)) {
617+
if contains_effect(expr, |id| checker.semantic().has_builtin_binding(id)) {
618618
return;
619619
}
620620

@@ -671,7 +671,7 @@ pub(crate) fn expr_or_not_expr(checker: &mut Checker, expr: &Expr) {
671671
return;
672672
}
673673

674-
if contains_effect(expr, |id| checker.semantic().is_builtin(id)) {
674+
if contains_effect(expr, |id| checker.semantic().has_builtin_binding(id)) {
675675
return;
676676
}
677677

@@ -748,14 +748,15 @@ fn is_short_circuit(
748748

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

755756
// Keep track of the location of the furthest-right, non-effectful expression.
756757
if value_truthiness.is_unknown()
757758
&& (!checker.semantic().in_boolean_test()
758-
|| contains_effect(value, |id| checker.semantic().is_builtin(id)))
759+
|| contains_effect(value, |id| checker.semantic().has_builtin_binding(id)))
759760
{
760761
furthest = next_value;
761762
continue;

crates/ruff_linter/src/rules/flake8_simplify/rules/ast_ifexp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ pub(crate) fn if_expr_with_true_false(
172172
.to_string(),
173173
expr.range(),
174174
)));
175-
} else if checker.semantic().is_builtin("bool") {
175+
} else if checker.semantic().has_builtin_binding("bool") {
176176
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement(
177177
checker.generator().expr(
178178
&ast::ExprCall {

crates/ruff_linter/src/rules/flake8_simplify/rules/ast_unary_op.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ pub(crate) fn double_negation(checker: &mut Checker, expr: &Expr, op: UnaryOp, o
270270
checker.locator().slice(operand.as_ref()).to_string(),
271271
expr.range(),
272272
)));
273-
} else if checker.semantic().is_builtin("bool") {
273+
} else if checker.semantic().has_builtin_binding("bool") {
274274
let node = ast::ExprName {
275275
id: "bool".into(),
276276
ctx: ExprContext::Load,

crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_dict_get.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ pub(crate) fn if_else_block_instead_of_dict_get(checker: &mut Checker, stmt_if:
161161
}
162162

163163
// Check that the default value is not "complex".
164-
if contains_effect(default_value, |id| checker.semantic().is_builtin(id)) {
164+
if contains_effect(default_value, |id| {
165+
checker.semantic().has_builtin_binding(id)
166+
}) {
165167
return;
166168
}
167169

@@ -261,7 +263,9 @@ pub(crate) fn if_exp_instead_of_dict_get(
261263
}
262264

263265
// Check that the default value is not "complex".
264-
if contains_effect(default_value, |id| checker.semantic().is_builtin(id)) {
266+
if contains_effect(default_value, |id| {
267+
checker.semantic().has_builtin_binding(id)
268+
}) {
265269
return;
266270
}
267271

crates/ruff_linter/src/rules/flake8_simplify/rules/if_else_block_instead_of_dict_lookup.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@ pub(crate) fn if_else_block_instead_of_dict_lookup(checker: &mut Checker, stmt_i
7777
return;
7878
};
7979

80-
if value
81-
.as_ref()
82-
.is_some_and(|value| contains_effect(value, |id| checker.semantic().is_builtin(id)))
83-
{
80+
if value.as_ref().is_some_and(|value| {
81+
contains_effect(value, |id| checker.semantic().has_builtin_binding(id))
82+
}) {
8483
return;
8584
}
8685

@@ -112,7 +111,7 @@ pub(crate) fn if_else_block_instead_of_dict_lookup(checker: &mut Checker, stmt_i
112111
return;
113112
};
114113
if value.as_ref().is_some_and(|value| {
115-
contains_effect(value, |id| checker.semantic().is_builtin(id))
114+
contains_effect(value, |id| checker.semantic().has_builtin_binding(id))
116115
}) {
117116
return;
118117
};
@@ -138,7 +137,7 @@ pub(crate) fn if_else_block_instead_of_dict_lookup(checker: &mut Checker, stmt_i
138137
};
139138

140139
if value.as_ref().is_some_and(|value| {
141-
contains_effect(value, |id| checker.semantic().is_builtin(id))
140+
contains_effect(value, |id| checker.semantic().has_builtin_binding(id))
142141
}) {
143142
return;
144143
};

crates/ruff_linter/src/rules/flake8_simplify/rules/needless_bool.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ pub(crate) fn needless_bool(checker: &mut Checker, stmt: &Stmt) {
207207
// If the condition is a comparison, we can replace it with the condition, since we
208208
// know it's a boolean.
209209
Some(if_test.clone())
210-
} else if checker.semantic().is_builtin("bool") {
210+
} else if checker.semantic().has_builtin_binding("bool") {
211211
// Otherwise, we need to wrap the condition in a call to `bool`.
212212
let func_node = ast::ExprName {
213213
id: "bool".into(),

crates/ruff_linter/src/rules/flake8_simplify/rules/reimplemented_builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ pub(crate) fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt) {
113113
},
114114
TextRange::new(stmt.start(), terminal.stmt.end()),
115115
);
116-
if checker.semantic().is_builtin("any") {
116+
if checker.semantic().has_builtin_binding("any") {
117117
diagnostic.set_fix(Fix::unsafe_edit(Edit::replacement(
118118
contents,
119119
stmt.start(),
@@ -199,7 +199,7 @@ pub(crate) fn convert_for_loop_to_any_all(checker: &mut Checker, stmt: &Stmt) {
199199
},
200200
TextRange::new(stmt.start(), terminal.stmt.end()),
201201
);
202-
if checker.semantic().is_builtin("all") {
202+
if checker.semantic().has_builtin_binding("all") {
203203
diagnostic.set_fix(Fix::unsafe_edit(Edit::replacement(
204204
contents,
205205
stmt.start(),

crates/ruff_linter/src/rules/pycodestyle/rules/type_comparison.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ fn deprecated_type_comparison(checker: &mut Checker, compare: &ast::ExprCompare)
134134
| "dict"
135135
| "set"
136136
| "memoryview"
137-
) && semantic.is_builtin(id)
137+
) && semantic.has_builtin_binding(id)
138138
{
139139
checker.diagnostics.push(Diagnostic::new(
140140
TypeComparison {
@@ -289,7 +289,7 @@ fn is_type(expr: &Expr, semantic: &SemanticModel) -> bool {
289289
| "ValueError"
290290
| "Warning"
291291
| "ZeroDivisionError"
292-
) && semantic.is_builtin(id)
292+
) && semantic.has_builtin_binding(id)
293293
}
294294
_ => false,
295295
}

crates/ruff_linter/src/rules/pyflakes/rules/unused_variable.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ fn remove_unused_variable(binding: &Binding, checker: &Checker) -> Option<Fix> {
219219
{
220220
if target.is_name_expr() {
221221
return if targets.len() > 1
222-
|| contains_effect(value, |id| checker.semantic().is_builtin(id))
222+
|| contains_effect(value, |id| checker.semantic().has_builtin_binding(id))
223223
{
224224
// If the expression is complex (`x = foo()`), remove the assignment,
225225
// but preserve the right-hand side.
@@ -265,7 +265,7 @@ fn remove_unused_variable(binding: &Binding, checker: &Checker) -> Option<Fix> {
265265
}) = statement
266266
{
267267
if target.is_name_expr() {
268-
return if contains_effect(value, |id| checker.semantic().is_builtin(id)) {
268+
return if contains_effect(value, |id| checker.semantic().has_builtin_binding(id)) {
269269
// If the expression is complex (`x = foo()`), remove the assignment,
270270
// but preserve the right-hand side.
271271
let start = statement.start();

crates/ruff_linter/src/rules/pylint/rules/if_stmt_min_max.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ pub(crate) fn if_stmt_min_max(checker: &mut Checker, stmt_if: &ast::StmtIf) {
177177
stmt_if.range(),
178178
);
179179

180-
if checker.semantic().is_builtin(min_max.as_str()) {
180+
if checker.semantic().has_builtin_binding(min_max.as_str()) {
181181
diagnostic.set_fix(Fix::safe_edit(Edit::range_replacement(
182182
replacement,
183183
stmt_if.range(),

0 commit comments

Comments
 (0)