Skip to content

Commit

Permalink
Fix various clippy warnings (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Phantomical committed Feb 2, 2024
1 parent 66ff2e4 commit 4b3f3b5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ on:
push:
pull_request:

name: ci

jobs:
test:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -41,6 +43,7 @@ jobs:
path: clippy.sarif

clippy-upload:
if: github.event_name == 'pull_request' || github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
needs: [clippy]
permissions:
Expand Down
18 changes: 7 additions & 11 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::error::Expected;
use crate::lex::{Lexer, Token, TokenKind};
use crate::Error;

//// A serde deserializer for rust's debug format.
/// A serde deserializer for rust's debug format.
pub struct Deserializer<'de> {
total: &'de str,
lexer: Lexer<'de>,
Expand Down Expand Up @@ -300,11 +300,11 @@ macro_rules! deserialize_unsigned {
{
let int = self.parse_integer()?;
let result = match int.value.get(..2) {
_ if int.sign == Sign::Negative => <$uint>::from_str_radix("-1", 10),
_ if int.sign == Sign::Negative => "-1".parse(),
Some("0x" | "0X") => <$uint>::from_str_radix(&int.value[2..], 16),
Some("0o" | "0O") => <$uint>::from_str_radix(&int.value[2..], 8),
Some("0b" | "0B") => <$uint>::from_str_radix(&int.value[2..], 2),
_ => <$uint>::from_str_radix(int.value, 10),
_ => int.value.parse(),
};

match result {
Expand Down Expand Up @@ -355,7 +355,7 @@ macro_rules! deserialize_signed {
};
}

impl<'a, 'de> serde::de::Deserializer<'de> for &'_ mut Deserializer<'de> {
impl<'de> serde::de::Deserializer<'de> for &'_ mut Deserializer<'de> {
type Error = Error;

fn is_human_readable(&self) -> bool {
Expand Down Expand Up @@ -757,12 +757,8 @@ impl<'de> MapAccess<'de> for DebugMapAccess<'_, 'de> {
where
K: DeserializeSeed<'de>,
{
match self.0.peek()? {
Token {
kind: TokenKind::Punct,
value: "}",
} => return Ok(None),
_ => (),
if self.0.peek()?.is_punct("}") {
return Ok(None);
}

seed.deserialize(&mut *self.0).map(Some)
Expand Down Expand Up @@ -888,7 +884,7 @@ impl<'de> VariantAccess<'de> for DebugEnumAccess<'_, 'de> {
}
}

fn unescape<'de>(mut text: &'de str) -> Result<Cow<'de, str>, Error> {
fn unescape(mut text: &str) -> Result<Cow<'_, str>, Error> {
let mut next = match text.find('\\') {
Some(pos) => pos,
None => return Ok(Cow::Borrowed(text)),
Expand Down
14 changes: 10 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ pub(crate) struct LexerError {
}

impl LexerError {
#[cold]
pub(crate) fn unexpected_token(found: &str, expected: impl Into<Expected>) -> Self {
Self {
found: found.into(),
expected: expected.into(),
}
}

#[cold]
pub(crate) fn unexpected_eof(expected: impl Into<Expected>) -> Self {
Self {
found: "".into(),
Expand All @@ -27,7 +29,7 @@ impl LexerError {

impl fmt::Display for LexerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.found == "" {
if self.found.is_empty() {
write!(f, "unexpected end of file, expected {}", self.expected)
} else {
write!(
Expand Down Expand Up @@ -69,27 +71,31 @@ pub(crate) use self::detail::Error as ErrorDetail;
pub struct Error(ErrorDetail);

impl Error {
#[cold]
pub(crate) fn parse_int(value: &str, error: std::num::ParseIntError) -> Self {
Self(ErrorDetail::ParseInt {
value: value.into(),
error,
})
}

#[cold]
pub(crate) fn parse_float(value: &str, error: std::num::ParseFloatError) -> Self {
Self(ErrorDetail::ParseFloat {
value: value.into(),
error,
})
}

#[cold]
pub(crate) fn unexpected_token(token: Token, expected: impl Into<Expected>) -> Self {
Self(ErrorDetail::Lexer(LexerError::unexpected_token(
token.value,
expected,
)))
}

#[cold]
pub(crate) fn invalid_string_literal(
_value: &str,
message: impl Into<Cow<'static, str>>,
Expand All @@ -100,19 +106,19 @@ impl Error {
}
}

impl<'de> From<LexerError> for Error {
impl From<LexerError> for Error {
fn from(error: LexerError) -> Self {
Self(ErrorDetail::Lexer(error))
}
}

impl<'de> fmt::Debug for Error {
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

impl<'de> fmt::Display for Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 {
ErrorDetail::Custom(msg) => f.write_str(msg),
Expand Down
32 changes: 18 additions & 14 deletions src/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ use std::fmt;

use crate::error::{Expected, LexerError};

#[derive(Copy, Clone, Debug)]
pub(crate) struct Token<'de> {
pub kind: TokenKind,
pub value: &'de str,
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub(crate) enum TokenKind {
/// An alphanumeric identifier token. It must start with a letter but then
Expand Down Expand Up @@ -61,6 +55,18 @@ pub(crate) enum TokenKind {
Eof,
}

#[derive(Copy, Clone, Debug)]
pub(crate) struct Token<'de> {
pub kind: TokenKind,
pub value: &'de str,
}

impl<'de> Token<'de> {
pub fn is_punct(&self, punct: &str) -> bool {
self.kind == TokenKind::Punct && self.value == punct
}
}

impl fmt::Display for TokenKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Expand Down Expand Up @@ -161,7 +167,7 @@ impl<'de> Lexer<'de> {
}

fn parse_string(&mut self) -> Result<TokenKind, LexerError> {
self.data = match self.data.strip_prefix("\"") {
self.data = match self.data.strip_prefix('"') {
Some(rest) => rest,
None => return Err(self.unexpected_token(TokenKind::String)),
};
Expand All @@ -176,7 +182,7 @@ impl<'de> Lexer<'de> {
break;
}

match self.data.as_bytes().get(0) {
match self.data.as_bytes().first() {
Some(b'\"') => {
self.advance(1);
Ok(TokenKind::String)
Expand All @@ -186,7 +192,7 @@ impl<'de> Lexer<'de> {
}

fn parse_char(&mut self) -> Result<TokenKind, LexerError> {
self.data = match self.data.strip_prefix("\'") {
self.data = match self.data.strip_prefix('\'') {
Some(rest) => rest,
None => return Err(self.unexpected_token(TokenKind::Char)),
};
Expand All @@ -201,7 +207,7 @@ impl<'de> Lexer<'de> {
break;
}

match self.data.as_bytes().get(0) {
match self.data.as_bytes().first() {
Some(b'\'') => {
self.advance(1);
Ok(TokenKind::Char)
Expand All @@ -221,8 +227,7 @@ impl<'de> Lexer<'de> {
.data
.char_indices()
.skip(1)
.skip_while(|&(_, c)| unicode_ident::is_xid_continue(c))
.next()
.find(|&(_, c)| !unicode_ident::is_xid_continue(c))
.map(|(idx, _)| idx)
.unwrap_or(self.data.len());

Expand Down Expand Up @@ -323,8 +328,7 @@ impl<'de> Lexer<'de> {
let index = self
.data
.char_indices()
.skip_while(|&(_, c)| pred(c))
.next()
.find(|&(_, c)| !pred(c))
.map(|(idx, _)| idx)
.unwrap_or(self.data.len());

Expand Down

0 comments on commit 4b3f3b5

Please sign in to comment.