Skip to content

Commit

Permalink
chore: Fix some violations of 'needless_pass_by_ref_mut' lint (#18795)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
osiewicz authored Oct 6, 2024
1 parent 59f0f4a commit 03c8446
Show file tree
Hide file tree
Showing 36 changed files with 158 additions and 204 deletions.
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
allow-private-module-inception = true
avoid-breaking-exported-api = false
2 changes: 1 addition & 1 deletion crates/assistant/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ impl Context {
cx: &mut ModelContext<Self>,
) -> Self {
let buffer = cx.new_model(|_cx| {
let mut buffer = Buffer::remote(
let buffer = Buffer::remote(
language::BufferId::new(1).unwrap(),
replica_id,
capability,
Expand Down
2 changes: 1 addition & 1 deletion crates/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ pub struct PendingEntitySubscription<T: 'static> {
}

impl<T: 'static> PendingEntitySubscription<T> {
pub fn set_model(mut self, model: &Model<T>, cx: &mut AsyncAppContext) -> Subscription {
pub fn set_model(mut self, model: &Model<T>, cx: &AsyncAppContext) -> Subscription {
self.consumed = true;
let mut handlers = self.client.handler_set.lock();
let id = (TypeId::of::<T>(), self.remote_id);
Expand Down
2 changes: 1 addition & 1 deletion crates/client/src/telemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl Telemetry {
system_id: Option<String>,
installation_id: Option<String>,
session_id: String,
cx: &mut AppContext,
cx: &AppContext,
) {
let mut state = self.state.lock();
state.system_id = system_id.map(|id| id.into());
Expand Down
38 changes: 15 additions & 23 deletions crates/client/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ enum UpdateContacts {
}

impl UserStore {
pub fn new(client: Arc<Client>, cx: &mut ModelContext<Self>) -> Self {
pub fn new(client: Arc<Client>, cx: &ModelContext<Self>) -> Self {
let (mut current_user_tx, current_user_rx) = watch::channel();
let (update_contacts_tx, mut update_contacts_rx) = mpsc::unbounded();
let rpc_subscriptions = vec![
Expand Down Expand Up @@ -310,7 +310,7 @@ impl UserStore {
fn update_contacts(
&mut self,
message: UpdateContacts,
cx: &mut ModelContext<Self>,
cx: &ModelContext<Self>,
) -> Task<Result<()>> {
match message {
UpdateContacts::Wait(barrier) => {
Expand Down Expand Up @@ -525,9 +525,9 @@ impl UserStore {
}

pub fn dismiss_contact_request(
&mut self,
&self,
requester_id: u64,
cx: &mut ModelContext<Self>,
cx: &ModelContext<Self>,
) -> Task<Result<()>> {
let client = self.client.upgrade();
cx.spawn(move |_, _| async move {
Expand Down Expand Up @@ -573,7 +573,7 @@ impl UserStore {
})
}

pub fn clear_contacts(&mut self) -> impl Future<Output = ()> {
pub fn clear_contacts(&self) -> impl Future<Output = ()> {
let (tx, mut rx) = postage::barrier::channel();
self.update_contacts_tx
.unbounded_send(UpdateContacts::Clear(tx))
Expand All @@ -583,7 +583,7 @@ impl UserStore {
}
}

pub fn contact_updates_done(&mut self) -> impl Future<Output = ()> {
pub fn contact_updates_done(&self) -> impl Future<Output = ()> {
let (tx, mut rx) = postage::barrier::channel();
self.update_contacts_tx
.unbounded_send(UpdateContacts::Wait(tx))
Expand All @@ -594,9 +594,9 @@ impl UserStore {
}

pub fn get_users(
&mut self,
&self,
user_ids: Vec<u64>,
cx: &mut ModelContext<Self>,
cx: &ModelContext<Self>,
) -> Task<Result<Vec<Arc<User>>>> {
let mut user_ids_to_fetch = user_ids.clone();
user_ids_to_fetch.retain(|id| !self.users.contains_key(id));
Expand Down Expand Up @@ -629,9 +629,9 @@ impl UserStore {
}

pub fn fuzzy_search_users(
&mut self,
&self,
query: String,
cx: &mut ModelContext<Self>,
cx: &ModelContext<Self>,
) -> Task<Result<Vec<Arc<User>>>> {
self.load_users(proto::FuzzySearchUsers { query }, cx)
}
Expand All @@ -640,11 +640,7 @@ impl UserStore {
self.users.get(&user_id).cloned()
}

pub fn get_user_optimistic(
&mut self,
user_id: u64,
cx: &mut ModelContext<Self>,
) -> Option<Arc<User>> {
pub fn get_user_optimistic(&self, user_id: u64, cx: &ModelContext<Self>) -> Option<Arc<User>> {
if let Some(user) = self.users.get(&user_id).cloned() {
return Some(user);
}
Expand All @@ -653,11 +649,7 @@ impl UserStore {
None
}

pub fn get_user(
&mut self,
user_id: u64,
cx: &mut ModelContext<Self>,
) -> Task<Result<Arc<User>>> {
pub fn get_user(&self, user_id: u64, cx: &ModelContext<Self>) -> Task<Result<Arc<User>>> {
if let Some(user) = self.users.get(&user_id).cloned() {
return Task::ready(Ok(user));
}
Expand Down Expand Up @@ -697,7 +689,7 @@ impl UserStore {
.map(|accepted_tos_at| accepted_tos_at.is_some())
}

pub fn accept_terms_of_service(&mut self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
pub fn accept_terms_of_service(&self, cx: &ModelContext<Self>) -> Task<Result<()>> {
if self.current_user().is_none() {
return Task::ready(Err(anyhow!("no current user")));
};
Expand Down Expand Up @@ -726,9 +718,9 @@ impl UserStore {
}

fn load_users(
&mut self,
&self,
request: impl RequestMessage<Response = UsersResponse>,
cx: &mut ModelContext<Self>,
cx: &ModelContext<Self>,
) -> Task<Result<Vec<Arc<User>>>> {
let client = self.client.clone();
cx.spawn(|this, mut cx| async move {
Expand Down
2 changes: 1 addition & 1 deletion crates/db/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ macro_rules! define_connection {
};
}

pub fn write_and_log<F>(cx: &mut AppContext, db_write: impl FnOnce() -> F + Send + 'static)
pub fn write_and_log<F>(cx: &AppContext, db_write: impl FnOnce() -> F + Send + 'static)
where
F: Future<Output = anyhow::Result<()>> + Send,
{
Expand Down
2 changes: 1 addition & 1 deletion crates/git_hosting_providers/src/git_hosting_providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use gpui::AppContext;
pub use crate::providers::*;

/// Initializes the Git hosting providers.
pub fn init(cx: &mut AppContext) {
pub fn init(cx: &AppContext) {
let provider_registry = GitHostingProviderRegistry::global(cx);

// The providers are stored in a `BTreeMap`, so insertion order matters.
Expand Down
22 changes: 9 additions & 13 deletions crates/gpui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -1004,19 +1004,15 @@ 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
}
/// 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(
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
}

Expand All @@ -1196,15 +1192,15 @@ 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());
}

/// Adds given path to the bottom of the list of recent paths for the application.
/// 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);
}

Expand Down
4 changes: 2 additions & 2 deletions crates/gpui/src/app/async_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions crates/gpui/src/app/model_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions crates/gpui/src/elements/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ impl Interactivity {
}

fn clamp_scroll_position(
&mut self,
&self,
bounds: Bounds<Pixels>,
style: &Style,
cx: &mut WindowContext,
Expand Down Expand Up @@ -1547,7 +1547,7 @@ impl Interactivity {

#[cfg(debug_assertions)]
fn paint_debug_info(
&mut self,
&self,
global_id: Option<&GlobalElementId>,
hitbox: &Hitbox,
style: &Style,
Expand Down
6 changes: 3 additions & 3 deletions crates/gpui/src/elements/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl TextLayout {
}

fn layout(
&mut self,
&self,
text: SharedString,
runs: Option<Vec<TextRun>>,
cx: &mut WindowContext,
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion crates/gpui/src/elements/uniform_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
8 changes: 2 additions & 6 deletions crates/gpui/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions crates/gpui/src/key_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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]>) {
Expand Down
2 changes: 1 addition & 1 deletion crates/gpui/src/platform/app_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {
Expand Down
Loading

0 comments on commit 03c8446

Please sign in to comment.