Skip to content

Commit f190a08

Browse files
authored
GEN-38: Minor improvements of code quality (#2885)
1 parent 3f626e0 commit f190a08

File tree

16 files changed

+55
-101
lines changed

16 files changed

+55
-101
lines changed

libs/error-stack/src/compat.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//! Compatibility module to convert errors from other libraries into [`Report`].
22
//!
33
//! In order to convert these error types, use [`IntoReportCompat::into_report()`].
4-
//!
5-
//! [`Report`]: crate::Report
64
75
use crate::Report;
86

@@ -19,7 +17,6 @@ mod eyre;
1917
/// would imply an implementation for [`Context`]. This also implies, that it's not possible to
2018
/// implement [`ResultExt`] from within `error-stack`.
2119
///
22-
/// [`Report`]: Report
2320
/// [`ResultExt`]: crate::ResultExt
2421
/// [`Context`]: crate::Context
2522
/// [`Error`]: core::error::Error

libs/error-stack/src/context.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
#[cfg(nightly)]
2-
use core::any::Demand;
3-
#[cfg(nightly)]
4-
use core::error::Error;
51
use core::fmt;
2+
#[cfg(nightly)]
3+
use core::{any::Demand, error::Error};
64
#[cfg(all(not(nightly), feature = "std"))]
75
use std::error::Error;
86

@@ -13,14 +11,11 @@ use crate::Report;
1311
/// When in a `std` environment or on a nightly toolchain, every [`Error`] is a valid `Context`.
1412
/// This trait is not limited to [`Error`]s and can also be manually implemented on a type.
1513
///
16-
/// [`Error`]: core::error::Error
17-
///
1814
/// ## Example
1915
///
2016
/// Used for creating a [`Report`] or for switching the [`Report`]'s context:
2117
///
2218
/// ```rust
23-
/// # #![cfg_attr(any(not(feature = "std"), miri), allow(unused_imports))]
2419
/// use std::{fmt, fs, io};
2520
///
2621
/// use error_stack::{Context, Result, ResultExt, Report};
@@ -32,7 +27,7 @@ use crate::Report;
3227
/// }
3328
///
3429
/// impl fmt::Display for ConfigError {
35-
/// # #[allow(unused_variables)]
30+
/// # #[allow(unused_variables)] // `fmt` is not used in this example
3631
/// fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
3732
/// # const _: &str = stringify! {
3833
/// ...
@@ -44,9 +39,6 @@ use crate::Report;
4439
/// // `Context` manually.
4540
/// impl Context for ConfigError {}
4641
///
47-
/// # #[cfg(any(not(feature = "std"), miri))]
48-
/// # pub fn read_file(_: &str) -> Result<String, ConfigError> { error_stack::bail!(ConfigError::ParseError) }
49-
/// # #[cfg(all(feature = "std", not(miri)))]
5042
/// pub fn read_file(path: &str) -> Result<String, io::Error> {
5143
/// // Creates a `Report` from `io::Error`, the current context is `io::Error`
5244
/// fs::read_to_string(path).map_err(Report::from)
@@ -62,7 +54,6 @@ use crate::Report;
6254
/// # }; Ok(())
6355
/// }
6456
/// # let err = parse_config("invalid-path").unwrap_err();
65-
/// # #[cfg(all(feature = "std", not(miri)))]
6657
/// # assert!(err.contains::<io::Error>());
6758
/// # assert!(err.contains::<ConfigError>());
6859
/// ```

libs/error-stack/src/fmt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ pub use hook::HookContext;
320320
#[cfg(any(feature = "std", feature = "hooks"))]
321321
pub(crate) use hook::{install_builtin_hooks, Format, Hooks};
322322
#[cfg(not(any(feature = "std", feature = "hooks")))]
323-
use location::LocationDisplay;
323+
use location::LocationAttachment;
324324

325325
use crate::{
326326
fmt::{
@@ -864,7 +864,7 @@ fn debug_attachments_invoke<'a>(
864864
FrameKind::Attachment(AttachmentKind::Opaque(_)) => frame
865865
.downcast_ref::<core::panic::Location<'static>>()
866866
.map(|location| {
867-
vec![LocationDisplay::new(location, config.color_mode()).to_string()]
867+
vec![LocationAttachment::new(location, config.color_mode()).to_string()]
868868
}),
869869
})
870870
.flat_map(|body| {

libs/error-stack/src/fmt/charset.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl Report<()> {
5858
///
5959
/// # Example
6060
///
61-
/// ```
61+
/// ```rust
6262
/// # // we only test the snapshot on nightly, therefore report is unused (so is render)
6363
/// # #![cfg_attr(not(nightly), allow(dead_code, unused_variables, unused_imports))]
6464
/// use std::io::{Error, ErrorKind};

libs/error-stack/src/fmt/color.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl Report<()> {
6767
///
6868
/// # Example
6969
///
70-
/// ```
70+
/// ```rust
7171
/// # // we only test the snapshot on nightly, therefore report is unused (so is render)
7272
/// # #![cfg_attr(not(nightly), allow(dead_code, unused_variables, unused_imports))]
7373
/// use std::io::{Error, ErrorKind};

libs/error-stack/src/fmt/config.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ pub(crate) struct Config {
4343

4444
#[cfg(not(any(feature = "std", feature = "hooks")))]
4545
impl Config {
46-
// alternate is still provided, as it is used in the hook counterpart
47-
#[allow(unused)]
48-
pub(crate) const fn new(color_mode: ColorMode, charset: Charset, alternate: bool) -> Self {
46+
pub(crate) const fn new(color_mode: ColorMode, charset: Charset, _alternate: bool) -> Self {
4947
Self {
5048
color_mode,
5149
charset,

libs/error-stack/src/fmt/hook.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ mod default {
468468
use tracing_error::SpanTrace;
469469

470470
use crate::{
471-
fmt::{hook::HookContext, location::LocationDisplay},
471+
fmt::{hook::HookContext, location::LocationAttachment},
472472
Report,
473473
};
474474

@@ -507,7 +507,7 @@ mod default {
507507
}
508508

509509
fn location(location: &Location<'static>, context: &mut HookContext<Location<'static>>) {
510-
context.push_body(LocationDisplay::new(location, context.color_mode()).to_string());
510+
context.push_body(LocationAttachment::new(location, context.color_mode()).to_string());
511511
}
512512

513513
#[cfg(all(feature = "std", rust_1_65))]

libs/error-stack/src/fmt/location.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
1-
use core::{
2-
fmt,
3-
fmt::{Display, Formatter},
4-
panic::Location,
5-
};
6-
7-
use crate::fmt::{
8-
color::{Color, DisplayStyle, Style},
9-
ColorMode,
10-
};
11-
12-
pub(super) struct LocationDisplay<'a> {
13-
location: &'a Location<'static>,
1+
use core::{fmt, panic::Location};
2+
3+
use crate::fmt::color::{Color, ColorMode, DisplayStyle, Style};
4+
5+
pub(super) struct LocationAttachment<'a, 'loc> {
6+
location: &'a Location<'loc>,
147
mode: ColorMode,
158
}
169

17-
impl<'a> LocationDisplay<'a> {
10+
impl<'a, 'loc> LocationAttachment<'a, 'loc> {
1811
#[must_use]
19-
pub(super) const fn new(location: &'a Location<'static>, mode: ColorMode) -> Self {
12+
pub(super) const fn new(location: &'a Location<'loc>, mode: ColorMode) -> Self {
2013
Self { location, mode }
2114
}
2215
}
2316

24-
impl<'a> Display for LocationDisplay<'a> {
25-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
17+
impl fmt::Display for LocationAttachment<'_, '_> {
18+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2619
let location = self.location;
2720

2821
let mut style = Style::new();

libs/error-stack/src/frame/frame_impl.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pub(super) trait FrameImpl: Send + Sync + 'static {
1818
fn provide<'a>(&'a self, demand: &mut Demand<'a>);
1919
}
2020

21-
#[repr(C)]
2221
struct ContextFrame<C> {
2322
context: C,
2423
}
@@ -42,7 +41,6 @@ impl<C: Context> FrameImpl for ContextFrame<C> {
4241
}
4342
}
4443

45-
#[repr(C)]
4644
struct AttachmentFrame<A> {
4745
attachment: A,
4846
}
@@ -66,7 +64,6 @@ impl<A: 'static + Send + Sync> FrameImpl for AttachmentFrame<A> {
6664
}
6765
}
6866

69-
#[repr(C)]
7067
struct PrintableAttachmentFrame<A> {
7168
attachment: A,
7269
}
@@ -210,6 +207,20 @@ impl Frame {
210207
}
211208
}
212209

210+
/// Creates a frame from an attachment which implements [`Debug`] and [`Display`].
211+
///
212+
/// [`Debug`]: core::fmt::Debug
213+
/// [`Display`]: core::fmt::Display
214+
pub(crate) fn from_printable_attachment<A>(attachment: A, sources: Box<[Self]>) -> Self
215+
where
216+
A: fmt::Display + fmt::Debug + Send + Sync + 'static,
217+
{
218+
Self {
219+
frame: Box::new(PrintableAttachmentFrame { attachment }),
220+
sources,
221+
}
222+
}
223+
213224
/// Creates a frame from an [`anyhow::Error`].
214225
#[cfg(feature = "anyhow")]
215226
pub(crate) fn from_anyhow(error: anyhow::Error, sources: Box<[Self]>) -> Self {
@@ -227,18 +238,4 @@ impl Frame {
227238
sources,
228239
}
229240
}
230-
231-
/// Creates a frame from an attachment which implements [`Debug`] and [`Display`].
232-
///
233-
/// [`Debug`]: core::fmt::Debug
234-
/// [`Display`]: core::fmt::Display
235-
pub(crate) fn from_printable_attachment<A>(attachment: A, sources: Box<[Self]>) -> Self
236-
where
237-
A: fmt::Display + fmt::Debug + Send + Sync + 'static,
238-
{
239-
Self {
240-
frame: Box::new(PrintableAttachmentFrame { attachment }),
241-
sources,
242-
}
243-
}
244241
}

libs/error-stack/src/hook.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl Report<()> {
2727
///
2828
/// # Examples
2929
///
30-
/// ```
30+
/// ```rust
3131
/// # // we only test the snapshot on nightly, therefore report is unused (so is render)
3232
/// # #![cfg_attr(not(nightly), allow(dead_code, unused_variables, unused_imports))]
3333
/// use std::io::{Error, ErrorKind};
@@ -71,7 +71,7 @@ impl Report<()> {
7171
/// This example showcases the ability of hooks to be invoked for values provided via the
7272
/// Provider API using [`Error::provide`].
7373
///
74-
/// ```
74+
/// ```rust
7575
/// # // this is a lot of boilerplate, if you find a better way, please change this!
7676
/// # // with #![cfg(nightly)] docsrs will complain that there's no main in non-nightly
7777
/// # #![cfg_attr(nightly, feature(error_generic_member_access, provide_any))]

0 commit comments

Comments
 (0)