Skip to content

Commit 20d7373

Browse files
committed
Avoid an unwrap in RustcMirAttrs::set_field.
1 parent 90c1409 commit 20d7373

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

Diff for: compiler/rustc_mir_dataflow/src/framework/graphviz.rs

+23-22
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,29 @@ impl RustcMirAttrs {
109109
.flat_map(|attr| attr.meta_item_list().into_iter().flat_map(|v| v.into_iter()));
110110

111111
for attr in rustc_mir_attrs {
112-
let attr_result = if attr.has_name(sym::borrowck_graphviz_postflow) {
113-
Self::set_field(&mut ret.basename_and_suffix, tcx, &attr, |s| {
114-
let path = PathBuf::from(s.to_string());
115-
match path.file_name() {
116-
Some(_) => Ok(path),
117-
None => {
118-
tcx.dcx().emit_err(PathMustEndInFilename { span: attr.span() });
112+
let attr_result = match attr.name() {
113+
Some(name @ sym::borrowck_graphviz_postflow) => {
114+
Self::set_field(&mut ret.basename_and_suffix, tcx, name, &attr, |s| {
115+
let path = PathBuf::from(s.to_string());
116+
match path.file_name() {
117+
Some(_) => Ok(path),
118+
None => {
119+
tcx.dcx().emit_err(PathMustEndInFilename { span: attr.span() });
120+
Err(())
121+
}
122+
}
123+
})
124+
}
125+
Some(name @ sym::borrowck_graphviz_format) => {
126+
Self::set_field(&mut ret.formatter, tcx, name, &attr, |s| match s {
127+
sym::gen_kill | sym::two_phase => Ok(s),
128+
_ => {
129+
tcx.dcx().emit_err(UnknownFormatter { span: attr.span() });
119130
Err(())
120131
}
121-
}
122-
})
123-
} else if attr.has_name(sym::borrowck_graphviz_format) {
124-
Self::set_field(&mut ret.formatter, tcx, &attr, |s| match s {
125-
sym::gen_kill | sym::two_phase => Ok(s),
126-
_ => {
127-
tcx.dcx().emit_err(UnknownFormatter { span: attr.span() });
128-
Err(())
129-
}
130-
})
131-
} else {
132-
Ok(())
132+
})
133+
}
134+
_ => Ok(()),
133135
};
134136

135137
result = result.and(attr_result);
@@ -141,13 +143,12 @@ impl RustcMirAttrs {
141143
fn set_field<T>(
142144
field: &mut Option<T>,
143145
tcx: TyCtxt<'_>,
146+
name: Symbol,
144147
attr: &ast::MetaItemInner,
145148
mapper: impl FnOnce(Symbol) -> Result<T, ()>,
146149
) -> Result<(), ()> {
147-
// Unwrapping the name is safe because this is only called when `has_name` has succeeded.
148150
if field.is_some() {
149-
tcx.dcx()
150-
.emit_err(DuplicateValuesFor { span: attr.span(), name: attr.name().unwrap() });
151+
tcx.dcx().emit_err(DuplicateValuesFor { span: attr.span(), name });
151152

152153
return Err(());
153154
}

0 commit comments

Comments
 (0)