diff options
| author | Jason Newcomb <jsnewcomb@pm.me> | 2021-03-25 09:25:04 -0400 |
|---|---|---|
| committer | Jason Newcomb <jsnewcomb@pm.me> | 2021-04-15 08:19:40 -0400 |
| commit | ce5e927713ddd32096f4e73f7a2c339cc8163d82 (patch) | |
| tree | 45caf3b8751de82133aa642e9b77715249a5ea48 /clippy_utils/src/source.rs | |
| parent | b1c675f3fc682201cdb28719133285b878e2d157 (diff) | |
| download | rust-ce5e927713ddd32096f4e73f7a2c339cc8163d82.tar.gz rust-ce5e927713ddd32096f4e73f7a2c339cc8163d82.zip | |
Improve `map_entry` lint
Fix false positives where the map is used before inserting into the map. Fix false positives where two insertions happen. Suggest using `if let Entry::Vacant(e) = _.entry(_)` when `or_insert` might be a semantic change
Diffstat (limited to 'clippy_utils/src/source.rs')
| -rw-r--r-- | clippy_utils/src/source.rs | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/clippy_utils/src/source.rs b/clippy_utils/src/source.rs index 2d794d48dc5..53180d1f9f5 100644 --- a/clippy_utils/src/source.rs +++ b/clippy_utils/src/source.rs @@ -66,6 +66,15 @@ pub fn indent_of<T: LintContext>(cx: &T, span: Span) -> Option<usize> { snippet_opt(cx, line_span(cx, span)).and_then(|snip| snip.find(|c: char| !c.is_whitespace())) } +/// Gets a snippet of the indentation of the line of a span +pub fn snippet_indent<T: LintContext>(cx: &T, span: Span) -> Option<String> { + snippet_opt(cx, line_span(cx, span)).map(|mut s| { + let len = s.len() - s.trim_start().len(); + s.truncate(len); + s + }) +} + // If the snippet is empty, it's an attribute that was inserted during macro // expansion and we want to ignore those, because they could come from external // sources that the user has no control over. |
