Skip to content

Commit f6125e4

Browse files
committed
Simplify RefTokenTreeCursor::look_ahead.
It's only ever used with a lookahead of 0, so this commit removes the lookahead and renames it `peek`.
1 parent a96ef96 commit f6125e4

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,8 @@ impl<'t> RefTokenTreeCursor<'t> {
678678
RefTokenTreeCursor { stream, index: 0 }
679679
}
680680

681-
pub fn look_ahead(&self, n: usize) -> Option<&TokenTree> {
682-
self.stream.0.get(self.index + n)
681+
pub fn peek(&self) -> Option<&TokenTree> {
682+
self.stream.0.get(self.index)
683683
}
684684
}
685685

compiler/rustc_expand/src/mbe/metavar_expr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl MetaVarExpr {
7474
}
7575
};
7676
result.push(element);
77-
if iter.look_ahead(0).is_none() {
77+
if iter.peek().is_none() {
7878
break;
7979
}
8080
if !try_eat_comma(&mut iter) {
@@ -166,7 +166,7 @@ fn parse_count<'psess>(
166166
eat_dollar(iter, psess, span)?;
167167
let ident = parse_ident(iter, psess, span)?;
168168
let depth = if try_eat_comma(iter) {
169-
if iter.look_ahead(0).is_none() {
169+
if iter.peek().is_none() {
170170
return Err(psess.dcx().struct_span_err(
171171
span,
172172
"`count` followed by a comma must have an associated index indicating its depth",
@@ -252,7 +252,7 @@ fn parse_token<'psess, 't>(
252252
/// Tries to move the iterator forward returning `true` if there is a comma. If not, then the
253253
/// iterator is not modified and the result is `false`.
254254
fn try_eat_comma(iter: &mut RefTokenTreeCursor<'_>) -> bool {
255-
if let Some(TokenTree::Token(Token { kind: token::Comma, .. }, _)) = iter.look_ahead(0) {
255+
if let Some(TokenTree::Token(Token { kind: token::Comma, .. }, _)) = iter.peek() {
256256
let _ = iter.next();
257257
return true;
258258
}
@@ -262,7 +262,7 @@ fn try_eat_comma(iter: &mut RefTokenTreeCursor<'_>) -> bool {
262262
/// Tries to move the iterator forward returning `true` if there is a dollar sign. If not, then the
263263
/// iterator is not modified and the result is `false`.
264264
fn try_eat_dollar(iter: &mut RefTokenTreeCursor<'_>) -> bool {
265-
if let Some(TokenTree::Token(Token { kind: token::Dollar, .. }, _)) = iter.look_ahead(0) {
265+
if let Some(TokenTree::Token(Token { kind: token::Dollar, .. }, _)) = iter.peek() {
266266
let _ = iter.next();
267267
return true;
268268
}
@@ -275,7 +275,7 @@ fn eat_dollar<'psess>(
275275
psess: &'psess ParseSess,
276276
span: Span,
277277
) -> PResult<'psess, ()> {
278-
if let Some(TokenTree::Token(Token { kind: token::Dollar, .. }, _)) = iter.look_ahead(0) {
278+
if let Some(TokenTree::Token(Token { kind: token::Dollar, .. }, _)) = iter.peek() {
279279
let _ = iter.next();
280280
return Ok(());
281281
}

src/tools/clippy/clippy_lints/src/crate_in_macro_def.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn contains_unhygienic_crate_reference(tts: &TokenStream) -> Option<Span> {
8686
while let Some(curr) = cursor.next() {
8787
if !prev_is_dollar
8888
&& let Some(span) = is_crate_keyword(curr)
89-
&& let Some(next) = cursor.look_ahead(0)
89+
&& let Some(next) = cursor.peek()
9090
&& is_token(next, &TokenKind::PathSep)
9191
{
9292
return Some(span);

src/tools/rustfmt/src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ impl<'a> MacroParser<'a> {
11901190
// (`(` ... `)` `=>` `{` ... `}`)*
11911191
fn parse(&mut self) -> Option<Macro> {
11921192
let mut branches = vec![];
1193-
while self.toks.look_ahead(0).is_some() {
1193+
while self.toks.peek().is_some() {
11941194
branches.push(self.parse_branch()?);
11951195
}
11961196

@@ -1237,7 +1237,7 @@ impl<'a> MacroParser<'a> {
12371237
span,
12381238
},
12391239
_,
1240-
)) = self.toks.look_ahead(0)
1240+
)) = self.toks.peek()
12411241
{
12421242
hi = span.hi();
12431243
self.toks.next();

0 commit comments

Comments
 (0)