chore: Fix some violations of 'needless_pass_by_ref_mut' lint (#18795)

While this lint is allow-by-default, it seems pretty useful to get rid
of mutable borrows when they're not needed.

Closes #ISSUE

Release Notes:

- N/A
This commit is contained in:
Piotr Osiewicz 2024-10-07 01:29:58 +02:00 committed by GitHub
parent 59f0f4ac42
commit 03c84466c2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
36 changed files with 158 additions and 204 deletions

View file

@ -348,7 +348,7 @@ impl AppContext {
}
/// Gracefully quit the application via the platform's standard routine.
pub fn quit(&mut self) {
pub fn quit(&self) {
self.platform.quit();
}
@ -1004,11 +1004,7 @@ impl AppContext {
self.globals_by_type.insert(global_type, lease.global);
}
pub(crate) fn new_view_observer(
&mut self,
key: TypeId,
value: NewViewListener,
) -> Subscription {
pub(crate) fn new_view_observer(&self, key: TypeId, value: NewViewListener) -> Subscription {
let (subscription, activate) = self.new_view_observers.insert(key, value);
activate();
subscription
@ -1016,7 +1012,7 @@ impl AppContext {
/// Arrange for the given function to be invoked whenever a view of the specified type is created.
/// The function will be passed a mutable reference to the view along with an appropriate context.
pub fn observe_new_views<V: 'static>(
&mut self,
&self,
on_new: impl 'static + Fn(&mut V, &mut ViewContext<V>),
) -> Subscription {
self.new_view_observer(
@ -1035,7 +1031,7 @@ impl AppContext {
/// Observe the release of a model or view. The callback is invoked after the model or view
/// has no more strong references but before it has been dropped.
pub fn observe_release<E, T>(
&mut self,
&self,
handle: &E,
on_release: impl FnOnce(&mut T, &mut AppContext) + 'static,
) -> Subscription
@ -1062,7 +1058,7 @@ impl AppContext {
mut f: impl FnMut(&KeystrokeEvent, &mut WindowContext) + 'static,
) -> Subscription {
fn inner(
keystroke_observers: &mut SubscriberSet<(), KeystrokeObserver>,
keystroke_observers: &SubscriberSet<(), KeystrokeObserver>,
handler: KeystrokeObserver,
) -> Subscription {
let (subscription, activate) = keystroke_observers.insert((), handler);
@ -1140,7 +1136,7 @@ impl AppContext {
/// Register a callback to be invoked when the application is about to quit.
/// It is not possible to cancel the quit event at this point.
pub fn on_app_quit<Fut>(
&mut self,
&self,
mut on_quit: impl FnMut(&mut AppContext) -> Fut + 'static,
) -> Subscription
where
@ -1186,7 +1182,7 @@ impl AppContext {
}
/// Sets the menu bar for this application. This will replace any existing menu bar.
pub fn set_menus(&mut self, menus: Vec<Menu>) {
pub fn set_menus(&self, menus: Vec<Menu>) {
self.platform.set_menus(menus, &self.keymap.borrow());
}
@ -1196,7 +1192,7 @@ impl AppContext {
}
/// Sets the right click menu for the app icon in the dock
pub fn set_dock_menu(&mut self, menus: Vec<MenuItem>) {
pub fn set_dock_menu(&self, menus: Vec<MenuItem>) {
self.platform.set_dock_menu(menus, &self.keymap.borrow());
}
@ -1204,7 +1200,7 @@ impl AppContext {
/// The list is usually shown on the application icon's context menu in the dock,
/// and allows to open the recent files via that context menu.
/// If the path is already in the list, it will be moved to the bottom of the list.
pub fn add_recent_document(&mut self, path: &Path) {
pub fn add_recent_document(&self, path: &Path) {
self.platform.add_recent_document(path);
}

View file

@ -107,7 +107,7 @@ impl Context for AsyncAppContext {
impl AsyncAppContext {
/// Schedules all windows in the application to be redrawn.
pub fn refresh(&mut self) -> Result<()> {
pub fn refresh(&self) -> Result<()> {
let app = self
.app
.upgrade()
@ -205,7 +205,7 @@ impl AsyncAppContext {
/// A convenience method for [AppContext::update_global]
/// for updating the global state of the specified type.
pub fn update_global<G: Global, R>(
&mut self,
&self,
update: impl FnOnce(&mut G, &mut AppContext) -> R,
) -> Result<R> {
let app = self

View file

@ -91,7 +91,7 @@ impl<'a, T: 'static> ModelContext<'a, T> {
/// Register a callback to be invoked when GPUI releases this model.
pub fn on_release(
&mut self,
&self,
on_release: impl FnOnce(&mut T, &mut AppContext) + 'static,
) -> Subscription
where
@ -110,7 +110,7 @@ impl<'a, T: 'static> ModelContext<'a, T> {
/// Register a callback to be run on the release of another model or view
pub fn observe_release<T2, E>(
&mut self,
&self,
entity: &E,
on_release: impl FnOnce(&mut T, &mut T2, &mut ModelContext<'_, T>) + 'static,
) -> Subscription
@ -154,7 +154,7 @@ impl<'a, T: 'static> ModelContext<'a, T> {
/// Arrange for the given function to be invoked whenever the application is quit.
/// The future returned from this callback will be polled for up to [crate::SHUTDOWN_TIMEOUT] until the app fully quits.
pub fn on_app_quit<Fut>(
&mut self,
&self,
mut on_quit: impl FnMut(&mut T, &mut ModelContext<T>) -> Fut + 'static,
) -> Subscription
where

View file

@ -1418,7 +1418,7 @@ impl Interactivity {
}
fn clamp_scroll_position(
&mut self,
&self,
bounds: Bounds<Pixels>,
style: &Style,
cx: &mut WindowContext,
@ -1547,7 +1547,7 @@ impl Interactivity {
#[cfg(debug_assertions)]
fn paint_debug_info(
&mut self,
&self,
global_id: Option<&GlobalElementId>,
hitbox: &Hitbox,
style: &Style,

View file

@ -252,7 +252,7 @@ impl TextLayout {
}
fn layout(
&mut self,
&self,
text: SharedString,
runs: Option<Vec<TextRun>>,
cx: &mut WindowContext,
@ -350,7 +350,7 @@ impl TextLayout {
layout_id
}
fn prepaint(&mut self, bounds: Bounds<Pixels>, text: &str) {
fn prepaint(&self, bounds: Bounds<Pixels>, text: &str) {
let mut element_state = self.lock();
let element_state = element_state
.as_mut()
@ -359,7 +359,7 @@ impl TextLayout {
element_state.bounds = Some(bounds);
}
fn paint(&mut self, text: &str, cx: &mut WindowContext) {
fn paint(&self, text: &str, cx: &mut WindowContext) {
let element_state = self.lock();
let element_state = element_state
.as_ref()

View file

@ -115,7 +115,7 @@ impl UniformListScrollHandle {
}
/// Scroll the list to the given item index.
pub fn scroll_to_item(&mut self, ix: usize) {
pub fn scroll_to_item(&self, ix: usize) {
self.0.borrow_mut().deferred_scroll_to_item = Some(ix);
}

View file

@ -706,11 +706,7 @@ pub struct Bounds<T: Clone + Default + Debug> {
impl Bounds<Pixels> {
/// Generate a centered bounds for the given display or primary display if none is provided
pub fn centered(
display_id: Option<DisplayId>,
size: Size<Pixels>,
cx: &mut AppContext,
) -> Self {
pub fn centered(display_id: Option<DisplayId>, size: Size<Pixels>, cx: &AppContext) -> Self {
let display = display_id
.and_then(|id| cx.find_display(id))
.or_else(|| cx.primary_display());
@ -730,7 +726,7 @@ impl Bounds<Pixels> {
}
/// Generate maximized bounds for the given display or primary display if none is provided
pub fn maximized(display_id: Option<DisplayId>, cx: &mut AppContext) -> Self {
pub fn maximized(display_id: Option<DisplayId>, cx: &AppContext) -> Self {
let display = display_id
.and_then(|id| cx.find_display(id))
.or_else(|| cx.primary_display());

View file

@ -219,7 +219,7 @@ impl DispatchTree {
self.focusable_node_ids.insert(focus_id, node_id);
}
pub fn parent_view_id(&mut self) -> Option<EntityId> {
pub fn parent_view_id(&self) -> Option<EntityId> {
self.view_stack.last().copied()
}
@ -484,7 +484,7 @@ impl DispatchTree {
/// Converts the longest prefix of input to a replay event and returns the rest.
fn replay_prefix(
&mut self,
&self,
mut input: SmallVec<[Keystroke; 1]>,
dispatch_path: &SmallVec<[DispatchNodeId; 32]>,
) -> (SmallVec<[Keystroke; 1]>, SmallVec<[Replay; 1]>) {

View file

@ -171,7 +171,7 @@ pub enum OsAction {
Redo,
}
pub(crate) fn init_app_menus(platform: &dyn Platform, cx: &mut AppContext) {
pub(crate) fn init_app_menus(platform: &dyn Platform, cx: &AppContext) {
platform.on_will_open_app_menu(Box::new({
let cx = cx.to_async();
move || {

View file

@ -284,11 +284,11 @@ impl MetalRenderer {
}
}
pub fn update_transparency(&mut self, _transparent: bool) {
pub fn update_transparency(&self, _transparent: bool) {
// todo(mac)?
}
pub fn destroy(&mut self) {
pub fn destroy(&self) {
// nothing to do
}
@ -486,7 +486,7 @@ impl MetalRenderer {
}
fn rasterize_paths(
&mut self,
&self,
paths: &[Path<ScaledPixels>],
instance_buffer: &mut InstanceBuffer,
instance_offset: &mut usize,
@ -576,7 +576,7 @@ impl MetalRenderer {
}
fn draw_shadows(
&mut self,
&self,
shadows: &[Shadow],
instance_buffer: &mut InstanceBuffer,
instance_offset: &mut usize,
@ -639,7 +639,7 @@ impl MetalRenderer {
}
fn draw_quads(
&mut self,
&self,
quads: &[Quad],
instance_buffer: &mut InstanceBuffer,
instance_offset: &mut usize,
@ -698,7 +698,7 @@ impl MetalRenderer {
}
fn draw_paths(
&mut self,
&self,
paths: &[Path<ScaledPixels>],
tiles_by_path_id: &HashMap<PathId, AtlasTile>,
instance_buffer: &mut InstanceBuffer,
@ -808,7 +808,7 @@ impl MetalRenderer {
}
fn draw_underlines(
&mut self,
&self,
underlines: &[Underline],
instance_buffer: &mut InstanceBuffer,
instance_offset: &mut usize,
@ -871,7 +871,7 @@ impl MetalRenderer {
}
fn draw_monochrome_sprites(
&mut self,
&self,
texture_id: AtlasTextureId,
sprites: &[MonochromeSprite],
instance_buffer: &mut InstanceBuffer,
@ -945,7 +945,7 @@ impl MetalRenderer {
}
fn draw_polychrome_sprites(
&mut self,
&self,
texture_id: AtlasTextureId,
sprites: &[PolychromeSprite],
instance_buffer: &mut InstanceBuffer,

View file

@ -1432,7 +1432,7 @@ impl UTType {
self.0
}
fn inner_mut(&mut self) -> *mut Object {
fn inner_mut(&self) -> *mut Object {
self.0 as *mut _
}
}

View file

@ -835,10 +835,7 @@ impl Window {
prompt: None,
})
}
fn new_focus_listener(
&mut self,
value: AnyWindowFocusListener,
) -> (Subscription, impl FnOnce()) {
fn new_focus_listener(&self, value: AnyWindowFocusListener) -> (Subscription, impl FnOnce()) {
self.focus_listeners.insert((), value)
}
}
@ -929,7 +926,7 @@ impl<'a> WindowContext<'a> {
/// Obtain a new [`FocusHandle`], which allows you to track and manipulate the keyboard focus
/// for elements rendered within this window.
pub fn focus_handle(&mut self) -> FocusHandle {
pub fn focus_handle(&self) -> FocusHandle {
FocusHandle::new(&self.window.focus_handles)
}
@ -1127,7 +1124,7 @@ impl<'a> WindowContext<'a> {
/// Register a callback to be invoked when the given Model or View is released.
pub fn observe_release<E, T>(
&mut self,
&self,
entity: &E,
mut on_release: impl FnOnce(&mut T, &mut WindowContext) + 'static,
) -> Subscription
@ -1155,7 +1152,7 @@ impl<'a> WindowContext<'a> {
}
/// Schedule the given closure to be run directly after the current frame is rendered.
pub fn on_next_frame(&mut self, callback: impl FnOnce(&mut WindowContext) + 'static) {
pub fn on_next_frame(&self, callback: impl FnOnce(&mut WindowContext) + 'static) {
RefCell::borrow_mut(&self.window.next_frame_callbacks).push(Box::new(callback));
}
@ -1165,7 +1162,7 @@ impl<'a> WindowContext<'a> {
/// It will cause the window to redraw on the next frame, even if no other changes have occurred.
///
/// If called from within a view, it will notify that view on the next frame. Otherwise, it will refresh the entire window.
pub fn request_animation_frame(&mut self) {
pub fn request_animation_frame(&self) {
let parent_id = self.parent_view_id();
self.on_next_frame(move |cx| {
if let Some(parent_id) = parent_id {
@ -1179,7 +1176,7 @@ impl<'a> WindowContext<'a> {
/// Spawn the future returned by the given closure on the application thread pool.
/// The closure is provided a handle to the current window and an `AsyncWindowContext` for
/// use within your future.
pub fn spawn<Fut, R>(&mut self, f: impl FnOnce(AsyncWindowContext) -> Fut) -> Task<R>
pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncWindowContext) -> Fut) -> Task<R>
where
R: 'static,
Fut: Future<Output = R> + 'static,
@ -2865,7 +2862,7 @@ impl<'a> WindowContext<'a> {
}
/// Get the last view id for the current element
pub fn parent_view_id(&mut self) -> Option<EntityId> {
pub fn parent_view_id(&self) -> Option<EntityId> {
self.window.next_frame.dispatch_tree.parent_view_id()
}
@ -3606,7 +3603,7 @@ impl<'a> WindowContext<'a> {
}
/// Updates the IME panel position suggestions for languages like japanese, chinese.
pub fn invalidate_character_coordinates(&mut self) {
pub fn invalidate_character_coordinates(&self) {
self.on_next_frame(|cx| {
if let Some(mut input_handler) = cx.window.platform_window.take_input_handler() {
if let Some(bounds) = input_handler.selected_bounds(cx) {
@ -3752,7 +3749,7 @@ impl<'a> WindowContext<'a> {
/// Register a callback that can interrupt the closing of the current window based the returned boolean.
/// If the callback returns false, the window won't be closed.
pub fn on_window_should_close(&mut self, f: impl Fn(&mut WindowContext) -> bool + 'static) {
pub fn on_window_should_close(&self, f: impl Fn(&mut WindowContext) -> bool + 'static) {
let mut this = self.to_async();
self.window
.platform_window
@ -4070,7 +4067,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
}
/// Sets a given callback to be run on the next frame.
pub fn on_next_frame(&mut self, f: impl FnOnce(&mut V, &mut ViewContext<V>) + 'static)
pub fn on_next_frame(&self, f: impl FnOnce(&mut V, &mut ViewContext<V>) + 'static)
where
V: 'static,
{
@ -4162,7 +4159,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
/// The callback receives a handle to the view's window. This handle may be
/// invalid, if the window was closed before the view was released.
pub fn on_release(
&mut self,
&self,
on_release: impl FnOnce(&mut V, AnyWindowHandle, &mut AppContext) + 'static,
) -> Subscription {
let window_handle = self.window.handle;
@ -4179,7 +4176,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
/// Register a callback to be invoked when the given Model or View is released.
pub fn observe_release<V2, E>(
&mut self,
&self,
entity: &E,
mut on_release: impl FnMut(&mut V, &mut V2, &mut ViewContext<'_, V>) + 'static,
) -> Subscription
@ -4212,7 +4209,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
/// Register a callback to be invoked when the window is resized.
pub fn observe_window_bounds(
&mut self,
&self,
mut callback: impl FnMut(&mut V, &mut ViewContext<V>) + 'static,
) -> Subscription {
let view = self.view.downgrade();
@ -4226,7 +4223,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
/// Register a callback to be invoked when the window is activated or deactivated.
pub fn observe_window_activation(
&mut self,
&self,
mut callback: impl FnMut(&mut V, &mut ViewContext<V>) + 'static,
) -> Subscription {
let view = self.view.downgrade();
@ -4240,7 +4237,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
/// Registers a callback to be invoked when the window appearance changes.
pub fn observe_window_appearance(
&mut self,
&self,
mut callback: impl FnMut(&mut V, &mut ViewContext<V>) + 'static,
) -> Subscription {
let view = self.view.downgrade();
@ -4260,7 +4257,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
mut f: impl FnMut(&mut V, &KeystrokeEvent, &mut ViewContext<V>) + 'static,
) -> Subscription {
fn inner(
keystroke_observers: &mut SubscriberSet<(), KeystrokeObserver>,
keystroke_observers: &SubscriberSet<(), KeystrokeObserver>,
handler: KeystrokeObserver,
) -> Subscription {
let (subscription, activate) = keystroke_observers.insert((), handler);
@ -4284,7 +4281,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
/// Register a callback to be invoked when the window's pending input changes.
pub fn observe_pending_input(
&mut self,
&self,
mut callback: impl FnMut(&mut V, &mut ViewContext<V>) + 'static,
) -> Subscription {
let view = self.view.downgrade();
@ -4372,7 +4369,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
/// and this callback lets you chose a default place to restore the users focus.
/// Returns a subscription and persists until the subscription is dropped.
pub fn on_focus_lost(
&mut self,
&self,
mut listener: impl FnMut(&mut V, &mut ViewContext<V>) + 'static,
) -> Subscription {
let view = self.view.downgrade();
@ -4418,10 +4415,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
/// The given callback is invoked with a [`WeakView<V>`] to avoid leaking the view for a long-running process.
/// It's also given an [`AsyncWindowContext`], which can be used to access the state of the view across await points.
/// The returned future will be polled on the main thread.
pub fn spawn<Fut, R>(
&mut self,
f: impl FnOnce(WeakView<V>, AsyncWindowContext) -> Fut,
) -> Task<R>
pub fn spawn<Fut, R>(&self, f: impl FnOnce(WeakView<V>, AsyncWindowContext) -> Fut) -> Task<R>
where
R: 'static,
Fut: Future<Output = R> + 'static,