mirror of
https://github.com/typst/typst
synced 2025-05-21 12:35:29 +08:00
Fix false positive for type/str comparison warning (#5957)
This commit is contained in:
parent
a754be513d
commit
4a78a7d082
@ -498,7 +498,7 @@ pub fn equal(lhs: &Value, rhs: &Value, sink: &mut dyn DeprecationSink) -> bool {
|
|||||||
|
|
||||||
// Type compatibility.
|
// Type compatibility.
|
||||||
(Type(ty), Str(str)) | (Str(str), Type(ty)) => {
|
(Type(ty), Str(str)) | (Str(str), Type(ty)) => {
|
||||||
warn_type_str_equal(sink);
|
warn_type_str_equal(sink, str);
|
||||||
ty.compat_name() == str.as_str()
|
ty.compat_name() == str.as_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -647,7 +647,9 @@ fn warn_type_str_join(sink: &mut dyn DeprecationSink) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cold]
|
#[cold]
|
||||||
fn warn_type_str_equal(sink: &mut dyn DeprecationSink) {
|
fn warn_type_str_equal(sink: &mut dyn DeprecationSink, s: &str) {
|
||||||
|
// Only warn if `s` looks like a type name to prevent false positives.
|
||||||
|
if is_compat_type_name(s) {
|
||||||
sink.emit_with_hints(
|
sink.emit_with_hints(
|
||||||
"comparing strings with types is deprecated",
|
"comparing strings with types is deprecated",
|
||||||
&[
|
&[
|
||||||
@ -655,6 +657,7 @@ fn warn_type_str_equal(sink: &mut dyn DeprecationSink) {
|
|||||||
"this comparison will always return `false` in future Typst releases",
|
"this comparison will always return `false` in future Typst releases",
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cold]
|
#[cold]
|
||||||
@ -672,3 +675,44 @@ fn warn_type_in_dict(sink: &mut dyn DeprecationSink) {
|
|||||||
&["this compatibility behavior only exists because `type` used to return a string"],
|
&["this compatibility behavior only exists because `type` used to return a string"],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_compat_type_name(s: &str) -> bool {
|
||||||
|
matches!(
|
||||||
|
s,
|
||||||
|
"boolean"
|
||||||
|
| "alignment"
|
||||||
|
| "angle"
|
||||||
|
| "arguments"
|
||||||
|
| "array"
|
||||||
|
| "bytes"
|
||||||
|
| "color"
|
||||||
|
| "content"
|
||||||
|
| "counter"
|
||||||
|
| "datetime"
|
||||||
|
| "decimal"
|
||||||
|
| "dictionary"
|
||||||
|
| "direction"
|
||||||
|
| "duration"
|
||||||
|
| "float"
|
||||||
|
| "fraction"
|
||||||
|
| "function"
|
||||||
|
| "gradient"
|
||||||
|
| "integer"
|
||||||
|
| "label"
|
||||||
|
| "length"
|
||||||
|
| "location"
|
||||||
|
| "module"
|
||||||
|
| "pattern"
|
||||||
|
| "ratio"
|
||||||
|
| "regex"
|
||||||
|
| "relative length"
|
||||||
|
| "selector"
|
||||||
|
| "state"
|
||||||
|
| "string"
|
||||||
|
| "stroke"
|
||||||
|
| "symbol"
|
||||||
|
| "tiling"
|
||||||
|
| "type"
|
||||||
|
| "version"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
@ -30,6 +30,8 @@
|
|||||||
// Hint: 7-26 compare with the literal type instead
|
// Hint: 7-26 compare with the literal type instead
|
||||||
// Hint: 7-26 this comparison will always return `false` in future Typst releases
|
// Hint: 7-26 this comparison will always return `false` in future Typst releases
|
||||||
#test(type(10) != "float", true)
|
#test(type(10) != "float", true)
|
||||||
|
// This is not a warning.
|
||||||
|
#test(type(10) in ("any", str, int), true)
|
||||||
|
|
||||||
--- type-string-compatibility-in-array ---
|
--- type-string-compatibility-in-array ---
|
||||||
// Warning: 7-35 comparing strings with types is deprecated
|
// Warning: 7-35 comparing strings with types is deprecated
|
||||||
|
Loading…
x
Reference in New Issue
Block a user