zed/tooling/lints/ui/owned_string_into_shared.rs
Miguel Raz Guzmán Macedo 4b7369481d
Add dylint lint library for Zed-specific patterns (#58496)
Adds a dylint library under tooling/lints that flags Zed-specific
anti-patterns:

* shared_string_from_str_literal, 
* async_block_without_await, 
* entity_update_in_render, 
* notify_in_render, 
* owned_string_into_shared, 
* len_in_loop_condition, and 
* blocking_io_on_foreground. 

Includes UI tests, a single-lint helper, and workspace.metadata.dylint
registration so cargo dylint --all discovers it. The library pins its
own nightly toolchain (kept out of the main workspace) and tracks dylint
6.

Release Notes:

- N/A

Self-Review Checklist:

- [ ] I've reviewed my own diff for quality, security, and reliability
- [ ] Unsafe blocks (if any) have justifying comments
- [ ] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [ ] Tests cover the new/changed behavior
- [ ] Performance impact has been considered and is acceptable

Closes #ISSUE

Release Notes:

- N/A or Added/Fixed/Improved ...
2026-07-03 22:05:34 +00:00

68 lines
2.2 KiB
Rust

// Tests for the `owned_string_into_shared` lint.
#![allow(unused)]
use std::borrow::Cow;
use std::rc::Rc;
use std::sync::Arc;
fn main() {
// --- Should warn ---
// String::from(<literal>).into() into Arc<str>.
let _a: Arc<str> = String::from("hello").into();
// String::from(<literal>).into() into Rc<str>.
let _b: Rc<str> = String::from("world").into();
// String::from(<literal>).into() into Cow<'_, str>.
let _c: Cow<'_, str> = String::from("borrowed-or-owned").into();
// <literal>.to_string().into() into Arc<str>.
let _d: Arc<str> = "via-to-string".to_string().into();
// <literal>.to_owned().into() into Arc<str>.
let _e: Arc<str> = "via-to-owned".to_owned().into();
// <literal>.to_string().into() into Rc<str>.
let _f: Rc<str> = "rc-via-to-string".to_string().into();
// <literal>.to_owned().into() into Cow<'_, str>.
let _g: Cow<'_, str> = "cow-via-to-owned".to_owned().into();
// Long literal still flagged the same way.
let _h: Arc<str> =
String::from("this literal is definitely longer than twenty three bytes").into();
// --- Should NOT warn ---
// Direct construction from the literal — already optimal.
let _ok1: Arc<str> = Arc::from("hello");
let _ok2: Rc<str> = Rc::from("world");
let _ok3: Cow<'_, str> = Cow::Borrowed("borrowed");
// Producing a plain `String` (not a refcounted destination).
let _ok4: String = String::from("not refcounted");
let _ok5: String = "x".to_string();
let _ok6: String = "x".to_owned();
// `.into()` from a non-literal `String` — the allocation is unavoidable.
let dynamic: String = make_string();
let _ok7: Arc<str> = dynamic.into();
// `.into()` from a `&str` directly (no owned `String` in between).
let _ok8: Arc<str> = "direct".into();
// `.into()` whose destination is not one of the targeted types.
let _ok9: Box<str> = String::from("box-str").into();
// `String::new()` is not built from a literal.
let _ok10: Arc<str> = String::new().into();
// Method call that is not `into`.
let _ok11: String = String::from("foo").clone();
}
fn make_string() -> String {
String::from("dynamic")
}