Skip to content

Commit f2cbe30

Browse files
committed
submod_path_from_attr: simplify & document
1 parent 564758c commit f2cbe30

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/librustc_parse/parser/module.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,22 @@ impl<'a> Parser<'a> {
179179
}
180180
}
181181

182+
/// Derive a submodule path from the first found `#[path = "path_string"]`.
183+
/// The provided `dir_path` is joined with the `path_string`.
182184
// Public for rustfmt usage.
183185
pub fn submod_path_from_attr(attrs: &[Attribute], dir_path: &Path) -> Option<PathBuf> {
184-
if let Some(s) = attr::first_attr_value_str_by_name(attrs, sym::path) {
185-
let s = s.as_str();
186+
// Extract path string from first `#[path = "path_string"]` attribute.
187+
let path_string = attr::first_attr_value_str_by_name(attrs, sym::path)?;
188+
let path_string = path_string.as_str();
186189

187-
// On windows, the base path might have the form
188-
// `\\?\foo\bar` in which case it does not tolerate
189-
// mixed `/` and `\` separators, so canonicalize
190-
// `/` to `\`.
191-
#[cfg(windows)]
192-
let s = s.replace("/", "\\");
193-
Some(dir_path.join(&*s))
194-
} else {
195-
None
196-
}
190+
// On windows, the base path might have the form
191+
// `\\?\foo\bar` in which case it does not tolerate
192+
// mixed `/` and `\` separators, so canonicalize
193+
// `/` to `\`.
194+
#[cfg(windows)]
195+
let path_string = path_string.replace("/", "\\");
196+
197+
Some(dir_path.join(&*path_string))
197198
}
198199

199200
/// Returns a path to a module.

0 commit comments

Comments
 (0)