Skip to content

Commit fb1d457

Browse files
authored
refactor(header): use better panic message in const HeaderName and HeaderValue (#797)
1 parent 20dbd6e commit fb1d457

File tree

2 files changed

+3
-58
lines changed

2 files changed

+3
-58
lines changed

src/header/name.rs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,27 +1205,6 @@ impl HeaderName {
12051205
///
12061206
/// This function panics when the static string is a invalid header.
12071207
///
1208-
/// Until [Allow panicking in constants](https://github.com/rust-lang/rfcs/pull/2345)
1209-
/// makes its way into stable, the panic message at compile-time is
1210-
/// going to look cryptic, but should at least point at your header value:
1211-
///
1212-
/// ```text
1213-
/// error: any use of this value will cause an error
1214-
/// --> http/src/header/name.rs:1241:13
1215-
/// |
1216-
/// 1241 | ([] as [u8; 0])[0]; // Invalid header name
1217-
/// | ^^^^^^^^^^^^^^^^^^
1218-
/// | |
1219-
/// | index out of bounds: the length is 0 but the index is 0
1220-
/// | inside `http::HeaderName::from_static` at http/src/header/name.rs:1241:13
1221-
/// | inside `INVALID_NAME` at src/main.rs:3:34
1222-
/// |
1223-
/// ::: src/main.rs:3:1
1224-
/// |
1225-
/// 3 | const INVALID_NAME: HeaderName = HeaderName::from_static("Capitalized");
1226-
/// | ------------------------------------------------------------------------
1227-
/// ```
1228-
///
12291208
/// # Examples
12301209
///
12311210
/// ```
@@ -1252,7 +1231,6 @@ impl HeaderName {
12521231
/// let a = HeaderName::from_static("foobar");
12531232
/// let b = HeaderName::from_static("FOOBAR"); // This line panics!
12541233
/// ```
1255-
#[allow(unconditional_panic)] // required for the panic circumvention
12561234
pub const fn from_static(src: &'static str) -> HeaderName {
12571235
let name_bytes = src.as_bytes();
12581236
if let Some(standard) = StandardHeader::from_bytes(name_bytes) {
@@ -1272,13 +1250,8 @@ impl HeaderName {
12721250
i += 1;
12731251
}
12741252
} {
1275-
// TODO: When msrv is bumped to larger than 1.57, this should be
1276-
// replaced with `panic!` macro.
1277-
// https://blog.rust-lang.org/2021/12/02/Rust-1.57.0.html#panic-in-const-contexts
1278-
//
1279-
// See the panics section of this method's document for details.
1280-
#[allow(clippy::no_effect, clippy::out_of_bounds_indexing)]
1281-
([] as [u8; 0])[0]; // Invalid header name
1253+
// Invalid header name
1254+
panic!("HeaderName::from_static with invalid bytes")
12821255
}
12831256

12841257
HeaderName {

src/header/value.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,6 @@ impl HeaderValue {
5151
/// This function panics if the argument contains invalid header value
5252
/// characters.
5353
///
54-
/// Until [Allow panicking in constants](https://github.com/rust-lang/rfcs/pull/2345)
55-
/// makes its way into stable, the panic message at compile-time is
56-
/// going to look cryptic, but should at least point at your header value:
57-
///
58-
/// ```text
59-
/// error: any use of this value will cause an error
60-
/// --> http/src/header/value.rs:67:17
61-
/// |
62-
/// 67 | ([] as [u8; 0])[0]; // Invalid header value
63-
/// | ^^^^^^^^^^^^^^^^^^
64-
/// | |
65-
/// | index out of bounds: the length is 0 but the index is 0
66-
/// | inside `HeaderValue::from_static` at http/src/header/value.rs:67:17
67-
/// | inside `INVALID_HEADER` at src/main.rs:73:33
68-
/// |
69-
/// ::: src/main.rs:73:1
70-
/// |
71-
/// 73 | const INVALID_HEADER: HeaderValue = HeaderValue::from_static("жsome value");
72-
/// | ----------------------------------------------------------------------------
73-
/// ```
74-
///
7554
/// # Examples
7655
///
7756
/// ```
@@ -80,19 +59,12 @@ impl HeaderValue {
8059
/// assert_eq!(val, "hello");
8160
/// ```
8261
#[inline]
83-
#[allow(unconditional_panic)] // required for the panic circumvention
8462
pub const fn from_static(src: &'static str) -> HeaderValue {
8563
let bytes = src.as_bytes();
8664
let mut i = 0;
8765
while i < bytes.len() {
8866
if !is_visible_ascii(bytes[i]) {
89-
// TODO: When msrv is bumped to larger than 1.57, this should be
90-
// replaced with `panic!` macro.
91-
// https://blog.rust-lang.org/2021/12/02/Rust-1.57.0.html#panic-in-const-contexts
92-
//
93-
// See the panics section of this method's document for details.
94-
#[allow(clippy::no_effect, clippy::out_of_bounds_indexing)]
95-
([] as [u8; 0])[0]; // Invalid header value
67+
panic!("HeaderValue::from_static with invalid bytes")
9668
}
9769
i += 1;
9870
}

0 commit comments

Comments
 (0)