summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2025-02-25 17:00:21 +0100
committerGitHub <noreply@github.com>2025-02-25 17:00:21 +0100
commit4a78a7d082aea2477411caea628d61b0b75be926 (patch)
treeca9ca07f8be4cdb305156c1cb409f55e24c8fb61
parenta754be513dd784149006bbbc8596177cdf8aa5ea (diff)
Fix false positive for type/str comparison warning (#5957)
-rw-r--r--crates/typst-library/src/foundations/ops.rs62
-rw-r--r--tests/suite/foundations/type.typ2
2 files changed, 55 insertions, 9 deletions
diff --git a/crates/typst-library/src/foundations/ops.rs b/crates/typst-library/src/foundations/ops.rs
index 4fa3c99e..c2cd5f5a 100644
--- a/crates/typst-library/src/foundations/ops.rs
+++ b/crates/typst-library/src/foundations/ops.rs
@@ -498,7 +498,7 @@ pub fn equal(lhs: &Value, rhs: &Value, sink: &mut dyn DeprecationSink) -> bool {
// Type compatibility.
(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()
}
@@ -647,14 +647,17 @@ fn warn_type_str_join(sink: &mut dyn DeprecationSink) {
}
#[cold]
-fn warn_type_str_equal(sink: &mut dyn DeprecationSink) {
- sink.emit_with_hints(
- "comparing strings with types is deprecated",
- &[
- "compare with the literal type instead",
- "this comparison will always return `false` in future Typst releases",
- ],
- );
+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(
+ "comparing strings with types is deprecated",
+ &[
+ "compare with the literal type instead",
+ "this comparison will always return `false` in future Typst releases",
+ ],
+ );
+ }
}
#[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"],
);
}
+
+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"
+ )
+}
diff --git a/tests/suite/foundations/type.typ b/tests/suite/foundations/type.typ
index 60f9d0ef..8f3dbea7 100644
--- a/tests/suite/foundations/type.typ
+++ b/tests/suite/foundations/type.typ
@@ -30,6 +30,8 @@
// Hint: 7-26 compare with the literal type instead
// Hint: 7-26 this comparison will always return `false` in future Typst releases
#test(type(10) != "float", true)
+// This is not a warning.
+#test(type(10) in ("any", str, int), true)
--- type-string-compatibility-in-array ---
// Warning: 7-35 comparing strings with types is deprecated