Skip to content

Commit

Permalink
Use Self everywhere (emilk#3787)
Browse files Browse the repository at this point in the history
This turns on the clippy lint
[`clippy::use_self`](https://rust-lang.github.io/rust-clippy/v0.0.212/index.html#use_self)
and fixes it everywhere.
  • Loading branch information
emilk authored Jan 8, 2024
1 parent 12ad9e7 commit 401de05
Show file tree
Hide file tree
Showing 72 changed files with 597 additions and 587 deletions.
32 changes: 26 additions & 6 deletions Cranky.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@
# cargo install cargo-cranky && cargo cranky
# See also clippy.toml

deny = [
"unsafe_code",
# Disabled waiting on https://github.com/rust-lang/rust-clippy/issues/9602
#"clippy::self_named_module_files",
]
deny = ["unsafe_code"]

warn = [
"clippy::all",
"clippy::as_ptr_cast_mut",
"clippy::await_holding_lock",
"clippy::bool_to_int_with_if",
"clippy::branches_sharing_code",
"clippy::char_lit_as_u8",
"clippy::checked_conversions",
"clippy::clear_with_drain",
"clippy::cloned_instead_of_copied",
"clippy::dbg_macro",
"clippy::debug_assert_with_mut_call",
"clippy::default_union_representation",
"clippy::derive_partial_eq_without_eq",
"clippy::disallowed_macros", # See clippy.toml
"clippy::disallowed_methods", # See clippy.toml
Expand All @@ -28,6 +26,7 @@ warn = [
"clippy::doc_link_with_quotes",
"clippy::doc_markdown",
"clippy::empty_enum",
"clippy::empty_line_after_outer_attr",
"clippy::enum_glob_use",
"clippy::equatable_if_let",
"clippy::exit",
Expand Down Expand Up @@ -90,14 +89,17 @@ warn = [
"clippy::nonstandard_macro_braces",
"clippy::option_option",
"clippy::path_buf_push_overwrite",
"clippy::print_stdout",
"clippy::ptr_as_ptr",
"clippy::ptr_cast_constness",
"clippy::pub_without_shorthand",
"clippy::rc_mutex",
"clippy::redundant_type_annotations",
"clippy::ref_option_ref",
"clippy::rest_pat_in_fully_bound_structs",
"clippy::same_functions_in_if_condition",
"clippy::semicolon_if_nothing_returned",
"clippy::significant_drop_tightening",
"clippy::single_match_else",
"clippy::str_to_string",
"clippy::string_add_assign",
Expand All @@ -109,21 +111,27 @@ warn = [
"clippy::todo",
"clippy::trailing_empty_array",
"clippy::trait_duplication_in_bounds",
"clippy::transmute_ptr_to_ptr",
"clippy::tuple_array_conversions",
"clippy::unchecked_duration_subtraction",
"clippy::undocumented_unsafe_blocks",
"clippy::unimplemented",
"clippy::uninlined_format_args",
"clippy::unnecessary_box_returns",
"clippy::unnecessary_safety_comment",
"clippy::unnecessary_safety_doc",
"clippy::unnecessary_self_imports",
"clippy::unnecessary_struct_initialization",
"clippy::unnecessary_wraps",
"clippy::unnested_or_patterns",
"clippy::unused_peekable",
"clippy::unused_rounding",
"clippy::unused_self",
"clippy::use_self",
"clippy::useless_transmute",
"clippy::verbose_file_reads",
"clippy::wildcard_dependencies",
"clippy::wildcard_imports",
"clippy::zero_sized_map_values",
"elided_lifetimes_in_paths",
"future_incompatible",
Expand All @@ -137,6 +145,14 @@ warn = [
"unused_extern_crates",
"unused_import_braces",
"unused_lifetimes",


# Enable when we update MSRV:
# "clippy::implied_bounds_in_impls",
# "clippy::needless_pass_by_ref_mut",
# "clippy::readonly_write_lock",
# "clippy::should_panic_without_expect",
# "clippy::string_lit_chars_any",
]

allow = [
Expand All @@ -148,9 +164,13 @@ allow = [
"clippy::let_underscore_untyped",
"clippy::missing_assert_message",
"clippy::missing_errors_doc",
"clippy::print_stderr", # TODO(emilk): use `log` crate instead
"clippy::self_named_module_files", # False positives
"clippy::too_many_lines",
"clippy::undocumented_unsafe_blocks",
"clippy::unwrap_used",
"clippy::wildcard_imports", # we do this a lot
"clippy::useless_let_if_seq", # False positives
"clippy::wildcard_imports", # We do this a lot
"trivial_casts",
"unused_qualifications",
]
20 changes: 10 additions & 10 deletions crates/ecolor/src/cint_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl From<Alpha<EncodedSrgb<u8>>> for Color32 {
alpha: a,
} = srgba;

Color32::from_rgba_unmultiplied(r, g, b, a)
Self::from_rgba_unmultiplied(r, g, b, a)
}
}

Expand All @@ -23,15 +23,15 @@ impl From<PremultipliedAlpha<EncodedSrgb<u8>>> for Color32 {
alpha: a,
} = srgba;

Color32::from_rgba_premultiplied(r, g, b, a)
Self::from_rgba_premultiplied(r, g, b, a)
}
}

impl From<Color32> for PremultipliedAlpha<EncodedSrgb<u8>> {
fn from(col: Color32) -> Self {
let (r, g, b, a) = col.to_tuple();

PremultipliedAlpha {
Self {
color: EncodedSrgb { r, g, b },
alpha: a,
}
Expand All @@ -51,7 +51,7 @@ impl From<PremultipliedAlpha<EncodedSrgb<f32>>> for Color32 {
let b = linear_u8_from_linear_f32(b);
let a = linear_u8_from_linear_f32(a);

Color32::from_rgba_premultiplied(r, g, b, a)
Self::from_rgba_premultiplied(r, g, b, a)
}
}

Expand All @@ -65,7 +65,7 @@ impl From<Color32> for PremultipliedAlpha<EncodedSrgb<f32>> {
let b = linear_f32_from_linear_u8(b);
let a = linear_f32_from_linear_u8(a);

PremultipliedAlpha {
Self {
color: EncodedSrgb { r, g, b },
alpha: a,
}
Expand All @@ -85,15 +85,15 @@ impl From<PremultipliedAlpha<LinearSrgb<f32>>> for Rgba {
alpha: a,
} = srgba;

Rgba([r, g, b, a])
Self([r, g, b, a])
}
}

impl From<Rgba> for PremultipliedAlpha<LinearSrgb<f32>> {
fn from(col: Rgba) -> Self {
let (r, g, b, a) = col.to_tuple();

PremultipliedAlpha {
Self {
color: LinearSrgb { r, g, b },
alpha: a,
}
Expand All @@ -113,15 +113,15 @@ impl From<Alpha<Hsv<f32>>> for Hsva {
alpha: a,
} = srgba;

Hsva::new(h, s, v, a)
Self::new(h, s, v, a)
}
}

impl From<Hsva> for Alpha<Hsv<f32>> {
fn from(col: Hsva) -> Self {
let Hsva { h, s, v, a } = col;

Alpha {
Self {
color: Hsv { h, s, v },
alpha: a,
}
Expand Down Expand Up @@ -153,7 +153,7 @@ impl From<HsvaGamma> for Alpha<Hsv<f32>> {
fn from(col: HsvaGamma) -> Self {
let Hsva { h, s, v, a } = col.into();

Alpha {
Self {
color: Hsv { h, s, v },
alpha: a,
}
Expand Down
50 changes: 25 additions & 25 deletions crates/ecolor/src/color32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,33 @@ impl std::ops::IndexMut<usize> for Color32 {
impl Color32 {
// Mostly follows CSS names:

pub const TRANSPARENT: Color32 = Color32::from_rgba_premultiplied(0, 0, 0, 0);
pub const BLACK: Color32 = Color32::from_rgb(0, 0, 0);
pub const DARK_GRAY: Color32 = Color32::from_rgb(96, 96, 96);
pub const GRAY: Color32 = Color32::from_rgb(160, 160, 160);
pub const LIGHT_GRAY: Color32 = Color32::from_rgb(220, 220, 220);
pub const WHITE: Color32 = Color32::from_rgb(255, 255, 255);
pub const TRANSPARENT: Self = Self::from_rgba_premultiplied(0, 0, 0, 0);
pub const BLACK: Self = Self::from_rgb(0, 0, 0);
pub const DARK_GRAY: Self = Self::from_rgb(96, 96, 96);
pub const GRAY: Self = Self::from_rgb(160, 160, 160);
pub const LIGHT_GRAY: Self = Self::from_rgb(220, 220, 220);
pub const WHITE: Self = Self::from_rgb(255, 255, 255);

pub const BROWN: Color32 = Color32::from_rgb(165, 42, 42);
pub const DARK_RED: Color32 = Color32::from_rgb(0x8B, 0, 0);
pub const RED: Color32 = Color32::from_rgb(255, 0, 0);
pub const LIGHT_RED: Color32 = Color32::from_rgb(255, 128, 128);
pub const BROWN: Self = Self::from_rgb(165, 42, 42);
pub const DARK_RED: Self = Self::from_rgb(0x8B, 0, 0);
pub const RED: Self = Self::from_rgb(255, 0, 0);
pub const LIGHT_RED: Self = Self::from_rgb(255, 128, 128);

pub const YELLOW: Color32 = Color32::from_rgb(255, 255, 0);
pub const LIGHT_YELLOW: Color32 = Color32::from_rgb(255, 255, 0xE0);
pub const KHAKI: Color32 = Color32::from_rgb(240, 230, 140);
pub const YELLOW: Self = Self::from_rgb(255, 255, 0);
pub const LIGHT_YELLOW: Self = Self::from_rgb(255, 255, 0xE0);
pub const KHAKI: Self = Self::from_rgb(240, 230, 140);

pub const DARK_GREEN: Color32 = Color32::from_rgb(0, 0x64, 0);
pub const GREEN: Color32 = Color32::from_rgb(0, 255, 0);
pub const LIGHT_GREEN: Color32 = Color32::from_rgb(0x90, 0xEE, 0x90);
pub const DARK_GREEN: Self = Self::from_rgb(0, 0x64, 0);
pub const GREEN: Self = Self::from_rgb(0, 255, 0);
pub const LIGHT_GREEN: Self = Self::from_rgb(0x90, 0xEE, 0x90);

pub const DARK_BLUE: Color32 = Color32::from_rgb(0, 0, 0x8B);
pub const BLUE: Color32 = Color32::from_rgb(0, 0, 255);
pub const LIGHT_BLUE: Color32 = Color32::from_rgb(0xAD, 0xD8, 0xE6);
pub const DARK_BLUE: Self = Self::from_rgb(0, 0, 0x8B);
pub const BLUE: Self = Self::from_rgb(0, 0, 255);
pub const LIGHT_BLUE: Self = Self::from_rgb(0xAD, 0xD8, 0xE6);

pub const GOLD: Color32 = Color32::from_rgb(255, 215, 0);
pub const GOLD: Self = Self::from_rgb(255, 215, 0);

pub const DEBUG_COLOR: Color32 = Color32::from_rgba_premultiplied(0, 200, 0, 128);
pub const DEBUG_COLOR: Self = Self::from_rgba_premultiplied(0, 200, 0, 128);

/// An ugly color that is planned to be replaced before making it to the screen.
///
Expand All @@ -69,10 +69,10 @@ impl Color32 {
///
/// This is used as a special color key,
/// i.e. often taken to mean "no color".
pub const PLACEHOLDER: Color32 = Color32::from_rgba_premultiplied(64, 254, 0, 128);
pub const PLACEHOLDER: Self = Self::from_rgba_premultiplied(64, 254, 0, 128);

#[deprecated = "Renamed to PLACEHOLDER"]
pub const TEMPORARY_COLOR: Color32 = Self::PLACEHOLDER;
pub const TEMPORARY_COLOR: Self = Self::PLACEHOLDER;

#[inline]
pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self {
Expand Down Expand Up @@ -198,7 +198,7 @@ impl Color32 {
///
/// This is perceptually even, and faster that [`Self::linear_multiply`].
#[inline]
pub fn gamma_multiply(self, factor: f32) -> Color32 {
pub fn gamma_multiply(self, factor: f32) -> Self {
crate::ecolor_assert!(0.0 <= factor && factor <= 1.0);
let Self([r, g, b, a]) = self;
Self([
Expand All @@ -214,7 +214,7 @@ impl Color32 {
/// This is using linear space, which is not perceptually even.
/// You may want to use [`Self::gamma_multiply`] instead.
#[inline]
pub fn linear_multiply(self, factor: f32) -> Color32 {
pub fn linear_multiply(self, factor: f32) -> Self {
crate::ecolor_assert!(0.0 <= factor && factor <= 1.0);
// As an unfortunate side-effect of using premultiplied alpha
// we need a somewhat expensive conversion to linear space and back.
Expand Down
21 changes: 9 additions & 12 deletions crates/ecolor/src/hex_color_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ impl FromStr for HexColor {
impl Display for HexColor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
HexColor::Hex3(color) => {
Self::Hex3(color) => {
let [r, g, b, _] = color.to_srgba_unmultiplied().map(|u| u >> 4);
f.write_fmt(format_args!("#{r:x}{g:x}{b:x}"))
}
HexColor::Hex4(color) => {
Self::Hex4(color) => {
let [r, g, b, a] = color.to_srgba_unmultiplied().map(|u| u >> 4);
f.write_fmt(format_args!("#{r:x}{g:x}{b:x}{a:x}"))
}
HexColor::Hex6(color) => {
Self::Hex6(color) => {
let [r, g, b, _] = color.to_srgba_unmultiplied();
let u = u32::from_be_bytes([0, r, g, b]);
f.write_fmt(format_args!("#{u:06x}"))
}
HexColor::Hex8(color) => {
Self::Hex8(color) => {
let [r, g, b, a] = color.to_srgba_unmultiplied();
let u = u32::from_be_bytes([r, g, b, a]);
f.write_fmt(format_args!("#{u:08x}"))
Expand All @@ -74,10 +74,7 @@ impl HexColor {
#[inline]
pub fn color(&self) -> Color32 {
match self {
HexColor::Hex3(color)
| HexColor::Hex4(color)
| HexColor::Hex6(color)
| HexColor::Hex8(color) => *color,
Self::Hex3(color) | Self::Hex4(color) | Self::Hex6(color) | Self::Hex8(color) => *color,
}
}

Expand All @@ -94,26 +91,26 @@ impl HexColor {
.map_err(ParseHexColorError::InvalidInt)?
.to_be_bytes();
let [r, g, b] = [r, gb >> 4, gb & 0x0f].map(|u| u << 4 | u);
Ok(HexColor::Hex3(Color32::from_rgb(r, g, b)))
Ok(Self::Hex3(Color32::from_rgb(r, g, b)))
}
4 => {
let [r_g, b_a] = u16::from_str_radix(s, 16)
.map_err(ParseHexColorError::InvalidInt)?
.to_be_bytes();
let [r, g, b, a] = [r_g >> 4, r_g & 0x0f, b_a >> 4, b_a & 0x0f].map(|u| u << 4 | u);
Ok(HexColor::Hex4(Color32::from_rgba_unmultiplied(r, g, b, a)))
Ok(Self::Hex4(Color32::from_rgba_unmultiplied(r, g, b, a)))
}
6 => {
let [_, r, g, b] = u32::from_str_radix(s, 16)
.map_err(ParseHexColorError::InvalidInt)?
.to_be_bytes();
Ok(HexColor::Hex6(Color32::from_rgb(r, g, b)))
Ok(Self::Hex6(Color32::from_rgb(r, g, b)))
}
8 => {
let [r, g, b, a] = u32::from_str_radix(s, 16)
.map_err(ParseHexColorError::InvalidInt)?
.to_be_bytes();
Ok(HexColor::Hex8(Color32::from_rgba_unmultiplied(r, g, b, a)))
Ok(Self::Hex8(Color32::from_rgba_unmultiplied(r, g, b, a)))
}
_ => Err(ParseHexColorError::InvalidLength)?,
}
Expand Down
Loading

0 comments on commit 401de05

Please sign in to comment.