Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 289c642

Browse files
Clarify code
Take advantage of the fact that very large regexes are unlikely
1 parent 2b9abc5 commit 289c642

File tree

1 file changed

+4
-17
lines changed

1 file changed

+4
-17
lines changed

clippy_lints/src/regex.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::syntax::ast::{LitKind, NodeId, StrStyle};
1818
use crate::syntax::source_map::{BytePos, Span};
1919
use crate::utils::{is_expn_of, match_def_path, match_type, opt_def_id, paths, span_help_and_lint, span_lint};
2020
use crate::consts::{constant, Constant};
21-
use std::convert::TryInto;
21+
use std::convert::TryFrom;
2222

2323
/// **What it does:** Checks [regex](https://crates.io/crates/regex) creation
2424
/// (with `Regex::new`,`RegexBuilder::new` or `RegexSet::new`) for correct
@@ -142,24 +142,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
142142
}
143143
}
144144

145+
#[allow(clippy::cast_possible_truncation)] // truncation very unlikely here
145146
fn str_span(base: Span, c: regex_syntax::ast::Span, offset: u16) -> Span {
146147
let offset = u32::from(offset);
147-
let end = base.lo() + BytePos(
148-
c.end
149-
.offset
150-
.try_into()
151-
.ok()
152-
.and_then(|o: u32| o.checked_add(offset))
153-
.expect("offset too large"),
154-
);
155-
let start = base.lo() + BytePos(
156-
c.start
157-
.offset
158-
.try_into()
159-
.ok()
160-
.and_then(|o: u32| o.checked_add(offset))
161-
.expect("offset too large"),
162-
);
148+
let end = base.lo() + BytePos(u32::try_from(c.end.offset).expect("offset too large") + offset);
149+
let start = base.lo() + BytePos(u32::try_from(c.start.offset).expect("offset too large") + offset);
163150
assert!(start <= end);
164151
Span::new(start, end, base.ctxt())
165152
}

0 commit comments

Comments
 (0)