Skip to content

Commit d242885

Browse files
authored
Tendril: fix warnings, lints and upgrade to edition 2021 (#665)
* Replace transmute with char::from_u32:unchecked Signed-off-by: Nico Burns <[email protected]> * Fix bare_trait_objects warning Signed-off-by: Nico Burns <[email protected]> * Fix clippy::useless_conversions Signed-off-by: Nico Burns <[email protected]> * Fix clippy::borrow_deref_ref Signed-off-by: Nico Burns <[email protected]> * Fix clippy::legacy_numeric_constants Signed-off-by: Nico Burns <[email protected]> * Fix clippy::map_clone Signed-off-by: Nico Burns <[email protected]> * Fix clippy::needless_lifetimes Signed-off-by: Nico Burns <[email protected]> * Fix clippy::explicit_auto_deref Signed-off-by: Nico Burns <[email protected]> * Fix clippy::len_zero Signed-off-by: Nico Burns <[email protected]> * Fix clippy::match_like_matches_macro Signed-off-by: Nico Burns <[email protected]> * Fix clippy::needless_return Signed-off-by: Nico Burns <[email protected]> * Fix clippy::while_let_loop Signed-off-by: Nico Burns <[email protected]> * Fix clippy::redundant_field_names Signed-off-by: Nico Burns <[email protected]> * Fix clippy::redundant_static_lifetimes Signed-off-by: Nico Burns <[email protected]> * Fix clippy::needless_late_init Signed-off-by: Nico Burns <[email protected]> * Fix clippy::op_ref Signed-off-by: Nico Burns <[email protected]> * Remove clippy::manual_repeat_n Signed-off-by: Nico Burns <[email protected]> * Fix clippy::ptr_offset_with_cast Signed-off-by: Nico Burns <[email protected]> * Fix clippy::partialeq_ne_impl Signed-off-by: Nico Burns <[email protected]> * Remove commented code in benchmark Signed-off-by: Nico Burns <[email protected]> * Upgrade tendril to Edition 2021 Signed-off-by: Nico Burns <[email protected]> * Fix clippy lints in tendril fuzz example Signed-off-by: Nico Burns <[email protected]> --------- Signed-off-by: Nico Burns <[email protected]>
1 parent df85634 commit d242885

File tree

12 files changed

+92
-159
lines changed

12 files changed

+92
-159
lines changed

tendril/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ authors = [
1010
license.workspace = true
1111
repository.workspace = true
1212
rust-version.workspace = true
13+
edition.workspace = true
1314
readme = "README.md"
14-
edition = "2015"
1515

1616
[dependencies]
1717
encoding = { workspace = true, optional = true}

tendril/benches/futf.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
extern crate criterion;
2-
extern crate tendril;
3-
41
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
52
use tendril::futf::classify;
63

tendril/benches/tendril.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@
44
// // option. This file may not be copied, modified, or distributed
55
// // except according to those terms.
66

7-
// use std::borrow::ToOwned;
8-
// use std::collections::hash_map::{Entry, HashMap};
9-
107
#![allow(clippy::manual_pattern_char_comparison)]
118

12-
extern crate criterion;
13-
extern crate tendril;
149
use std::collections::{hash_map::Entry, HashMap};
1510

1611
use criterion::{criterion_group, criterion_main, Bencher, Criterion};

tendril/examples/fuzz.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
//! A simple fuzz tester for the library.
88
99
#![deny(warnings)]
10-
#![allow(clippy::redundant_static_lifetimes)]
11-
#![allow(clippy::needless_borrow)]
12-
#![allow(clippy::borrow_deref_ref)]
13-
14-
extern crate rand;
15-
extern crate tendril;
1610

1711
use std::borrow::ToOwned;
1812

@@ -48,8 +42,8 @@ fn fuzz() {
4842
16..=31 => {
4943
let (start, end) = random_slice(&mut rng, &buf_string);
5044
let snip = &buf_string[start..end].to_owned();
51-
buf_string.push_str(&snip);
52-
buf_tendril.push_slice(&snip);
45+
buf_string.push_str(snip);
46+
buf_tendril.push_slice(snip);
5347
assert_eq!(&*buf_string, &*buf_tendril);
5448
},
5549

@@ -132,8 +126,7 @@ fn random_slice<R: Rng>(rng: &mut R, text: &str) -> (usize, usize) {
132126
}
133127
}
134128

135-
static TEXT: &'static str =
136-
"It was from the artists and poets that the pertinent answers came, and I \
129+
static TEXT: &str = "It was from the artists and poets that the pertinent answers came, and I \
137130
know that panic would have broken loose had they been able to compare notes. \
138131
As it was, lacking their original letters, I half suspected the compiler of \
139132
having asked leading questions, or of having edited the correspondence in \

tendril/src/buf32.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66

77
//! Provides an unsafe owned buffer type, used in implementing `Tendril`.
88
9-
use std::{mem, ptr, slice, u32};
9+
use std::{mem, ptr, slice};
1010

11-
use OFLOW;
11+
use crate::OFLOW;
1212

1313
pub const MIN_CAP: u32 = 16;
14-
1514
pub const MAX_LEN: usize = u32::MAX as usize;
1615

1716
/// A buffer points to a header of type `H`, which is followed by `MIN_CAP` or more
@@ -43,11 +42,7 @@ impl<H> Buf32<H> {
4342
mem::forget(vec);
4443
ptr::write(ptr, h);
4544

46-
Buf32 {
47-
ptr: ptr,
48-
len: 0,
49-
cap: cap,
50-
}
45+
Buf32 { ptr, len: 0, cap }
5146
}
5247

5348
#[inline]
@@ -61,7 +56,7 @@ impl<H> Buf32<H> {
6156

6257
#[inline(always)]
6358
pub unsafe fn data_ptr(&self) -> *mut u8 {
64-
(self.ptr as *mut u8).offset(mem::size_of::<H>() as isize)
59+
(self.ptr as *mut u8).add(mem::size_of::<H>())
6560
}
6661

6762
#[inline(always)]

tendril/src/fmt.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
use std::default::Default;
2323
use std::{char, mem, str};
2424

25-
use futf::{self, Codepoint, Meaning};
25+
use crate::futf::{self, Codepoint, Meaning};
2626

2727
/// Implementation details.
2828
///
2929
/// You don't need these unless you are implementing
3030
/// a new format.
3131
pub mod imp {
3232
use std::default::Default;
33-
use std::{iter, mem, slice};
33+
use std::{iter, slice};
3434

3535
/// Describes how to fix up encodings when concatenating.
3636
///
@@ -55,11 +55,6 @@ pub mod imp {
5555
}
5656
}
5757

58-
#[inline(always)]
59-
unsafe fn from_u32_unchecked(n: u32) -> char {
60-
mem::transmute(n)
61-
}
62-
6358
pub struct SingleByteCharIndices<'a> {
6459
inner: iter::Enumerate<slice::Iter<'a, u8>>,
6560
}
@@ -71,7 +66,7 @@ pub mod imp {
7166
fn next(&mut self) -> Option<(usize, char)> {
7267
self.inner
7368
.next()
74-
.map(|(i, &b)| unsafe { (i, from_u32_unchecked(b as u32)) })
69+
.map(|(i, &b)| unsafe { (i, char::from_u32_unchecked(b as u32)) })
7570
}
7671
}
7772

@@ -293,30 +288,30 @@ unsafe impl Format for UTF8 {
293288

294289
#[inline]
295290
fn validate_prefix(buf: &[u8]) -> bool {
296-
if buf.len() == 0 {
291+
if buf.is_empty() {
297292
return true;
298293
}
299-
match futf::classify(buf, buf.len() - 1) {
294+
matches!(
295+
futf::classify(buf, buf.len() - 1),
300296
Some(Codepoint {
301297
meaning: Meaning::Whole(_),
302298
..
303-
}) => true,
304-
_ => false,
305-
}
299+
})
300+
)
306301
}
307302

308303
#[inline]
309304
fn validate_suffix(buf: &[u8]) -> bool {
310-
if buf.len() == 0 {
305+
if buf.is_empty() {
311306
return true;
312307
}
313-
match futf::classify(buf, 0) {
308+
matches!(
309+
futf::classify(buf, 0),
314310
Some(Codepoint {
315311
meaning: Meaning::Whole(_),
316312
..
317-
}) => true,
318-
_ => false,
319-
}
313+
})
314+
)
320315
}
321316

322317
#[inline]
@@ -374,10 +369,10 @@ pub struct WTF8;
374369

375370
#[inline]
376371
fn wtf8_meaningful(m: Meaning) -> bool {
377-
match m {
378-
Meaning::Whole(_) | Meaning::LeadSurrogate(_) | Meaning::TrailSurrogate(_) => true,
379-
_ => false,
380-
}
372+
matches!(
373+
m,
374+
Meaning::Whole(_) | Meaning::LeadSurrogate(_) | Meaning::TrailSurrogate(_)
375+
)
381376
}
382377

383378
unsafe impl Format for WTF8 {
@@ -405,7 +400,7 @@ unsafe impl Format for WTF8 {
405400

406401
#[inline]
407402
fn validate_prefix(buf: &[u8]) -> bool {
408-
if buf.len() == 0 {
403+
if buf.is_empty() {
409404
return true;
410405
}
411406
match futf::classify(buf, buf.len() - 1) {
@@ -416,7 +411,7 @@ unsafe impl Format for WTF8 {
416411

417412
#[inline]
418413
fn validate_suffix(buf: &[u8]) -> bool {
419-
if buf.len() == 0 {
414+
if buf.is_empty() {
420415
return true;
421416
}
422417
match futf::classify(buf, 0) {
@@ -432,7 +427,7 @@ unsafe impl Format for WTF8 {
432427

433428
#[inline]
434429
unsafe fn fixup(lhs: &[u8], rhs: &[u8]) -> imp::Fixup {
435-
const ERR: &'static str = "WTF8: internal error";
430+
const ERR: &str = "WTF8: internal error";
436431

437432
if lhs.len() >= 3 && rhs.len() >= 3 {
438433
if let (

tendril/src/futf.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// option. This file may not be copied, modified, or distributed
55
// except according to those terms.
66

7+
use debug_unreachable::debug_unreachable;
78
use std::{char, slice};
89

910
/// Meaning of a complete or partial UTF-8 codepoint.
@@ -134,10 +135,10 @@ unsafe fn decode(buf: &[u8]) -> Option<Meaning> {
134135
}
135136

136137
#[inline(always)]
137-
unsafe fn unsafe_slice<'a>(buf: &'a [u8], start: usize, new_len: usize) -> &'a [u8] {
138+
unsafe fn unsafe_slice(buf: &[u8], start: usize, new_len: usize) -> &[u8] {
138139
debug_assert!(start <= buf.len());
139140
debug_assert!(new_len <= (buf.len() - start));
140-
slice::from_raw_parts(buf.as_ptr().offset(start as isize), new_len)
141+
slice::from_raw_parts(buf.as_ptr().add(start), new_len)
141142
}
142143

143144
/// Describes the UTF-8 codepoint containing the byte at index `idx` within
@@ -168,9 +169,9 @@ pub fn classify<'a>(buf: &'a [u8], idx: usize) -> Option<Codepoint<'a>> {
168169
}
169170
let meaning = decode(bytes)?;
170171
Some(Codepoint {
171-
bytes: bytes,
172+
bytes,
172173
rewind: 0,
173-
meaning: meaning,
174+
meaning,
174175
})
175176
} else {
176177
Some(Codepoint {
@@ -208,9 +209,9 @@ pub fn classify<'a>(buf: &'a [u8], idx: usize) -> Option<Codepoint<'a>> {
208209
}
209210
let meaning = decode(bytes)?;
210211
return Some(Codepoint {
211-
bytes: bytes,
212+
bytes,
212213
rewind: idx - start,
213-
meaning: meaning,
214+
meaning,
214215
});
215216
} else {
216217
return Some(Codepoint {
@@ -355,7 +356,7 @@ mod tests {
355356
}
356357
}
357358

358-
static JUNK: &'static [u8] = b"\
359+
static JUNK: &[u8] = b"\
359360
\xf8\x0d\x07\x25\xa6\x7b\x95\xeb\x47\x01\x7f\xee\
360361
\x3b\x00\x60\x57\x1d\x9e\x5d\x0a\x0b\x0a\x7c\x75\
361362
\x13\xa1\x82\x46\x27\x34\xe9\x52\x61\x0d\xec\x10\

tendril/src/lib.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,14 @@
55
// except according to those terms.
66

77
//#![cfg_attr(test, deny(warnings))]
8-
#![allow(unnecessary_transmutes)]
9-
#![allow(bare_trait_objects)]
10-
#![allow(clippy::ptr_offset_with_cast)]
11-
#![allow(clippy::needless_lifetimes)]
12-
#![allow(clippy::needless_late_init)]
13-
#![allow(clippy::explicit_auto_deref)]
148
#![allow(clippy::result_unit_err)]
15-
#![allow(clippy::op_ref)]
169
#![allow(clippy::missing_safety_doc)]
1710
#![allow(clippy::missing_transmute_annotations)]
18-
#![allow(clippy::partialeq_ne_impl)]
19-
#![allow(clippy::legacy_numeric_constants)]
2011
#![allow(clippy::collapsible_if)]
2112
#![allow(clippy::wrong_self_convention)]
22-
#![allow(clippy::len_zero)]
2313
#![allow(clippy::transmute_bytes_to_str)]
24-
#![allow(clippy::match_like_matches_macro)]
25-
#![allow(clippy::redundant_static_lifetimes)]
26-
#![allow(clippy::redundant_field_names)]
2714
#![allow(clippy::unusual_byte_groupings)]
28-
#![allow(clippy::borrow_deref_ref)]
29-
#![allow(clippy::needless_return)]
30-
#![allow(clippy::while_let_loop)]
3115
#![allow(clippy::mutable_key_type)]
32-
#![allow(clippy::manual_repeat_n)]
33-
#![allow(clippy::map_clone)]
34-
#![allow(clippy::useless_conversion)]
35-
36-
#[macro_use]
37-
extern crate debug_unreachable;
38-
#[cfg(feature = "encoding")]
39-
pub extern crate encoding;
40-
#[cfg(feature = "encoding_rs")]
41-
pub extern crate encoding_rs;
42-
extern crate utf8;
4316

4417
pub use fmt::Format;
4518
pub use stream::TendrilSink;
@@ -59,4 +32,4 @@ mod util;
5932
#[doc(hidden)]
6033
pub mod futf;
6134

62-
static OFLOW: &'static str = "tendril: overflow in buffer arithmetic";
35+
static OFLOW: &str = "tendril: overflow in buffer arithmetic";

0 commit comments

Comments
 (0)