rust-analyzer
rust-analyzer copied to clipboard
Incorrect diagnostics for Builders generated from derive_builder
Given the following Cargo.toml and main.rs
[package]
name = "analyzer_bug"
version = "0.1.0"
authors = ["Person McPerson <[email protected]>"]
edition = "2018"
[dependencies]
atom_syndication = {version = "0.12.2", features = ["derive_builder"]}
use atom_syndication::PersonBuilder;
fn main() {
let person = PersonBuilder::default()
.uri("https://person.org".to_string())
.email("[email protected]".to_string())
.name("Person McPerson".to_string())
.build();
println!("{}", person.name);
}
I would expect no diagnostics to be reported. However, running rust-analyzer produces the following diagnostics:
2024-02-21 22:54:46 PST reproduce@kdevenv # rust-analyzer diagnostics .
processing crate: analyzer_bug, module: /home/devuser/sandbox/reproduce/src/main.rs
Diagnostic { code: RustcHardError("E0308"), message: "expected Option<String>, found String", r
ange: FileRange { file_id: FileId(11), range: 173..182 }, severity: Error, unused: false, exper
imental: false, fixes: Some([Assist { id: AssistId("wrap_in_constructor", QuickFix), label: "Wr
ap in Some", group: None, target: 153..184, source_change: Some(SourceChange { source_file_edit
s: {FileId(11): (TextEdit { indels: [Indel { insert: "Some(", delete: 153..153 }, Indel { inser
t: ")", delete: 184..184 }] }, None)}, file_system_edits: [], is_snippet: false }), trigger_sig
nature_help: false }]), main_node: None }
Diagnostic { code: RustcHardError("E0308"), message: "expected Option<String>, found String", r
ange: FileRange { file_id: FileId(11), range: 125..134 }, severity: Error, unused: false, exper
imental: false, fixes: Some([Assist { id: AssistId("wrap_in_constructor", QuickFix), label: "Wr
ap in Some", group: None, target: 104..136, source_change: Some(SourceChange { source_file_edit
s: {FileId(11): (TextEdit { indels: [Indel { insert: "Some(", delete: 104..104 }, Indel { inser
t: ")", delete: 136..136 }] }, None)}, file_system_edits: [], is_snippet: false }), trigger_sig
nature_help: false }]), main_node: None }
diagnostic scan complete
Error: diagnostic error detected
The Person struct has several fields which are of type Option<String>. However, the generated builder has functions which will accept anything that implements Into<Option<String>>. But apparently Rust analyzer doesn't understand this and assumes that the arguments provided to .uri and .email must be exactly of type Option<String>.
rust-analyzer version: rust-analyzer 1 (68c506fd62 2024-02-19)
rustc version: rustc 1.76.0 (07dca489a 2024-02-04)