diff --git a/cli/diff.rs b/cli/diff.rs index f5596bef53..9aa0b5a441 100644 --- a/cli/diff.rs +++ b/cli/diff.rs @@ -2,6 +2,7 @@ use crate::colors; use dissimilar::{diff as difference, Chunk}; +use std::fmt::Write as _; /// Print diff of the same file_path, before and after formatting. /// @@ -113,12 +114,14 @@ impl DiffBuilder { fn write_line_diff(&mut self) { let split = self.orig.split('\n').enumerate(); for (i, s) in split { - self.output.push_str(&format!( + write!( + self.output, "{:width$}{} ", self.orig_line + i, colors::gray(" |"), width = self.line_number_width - )); + ) + .unwrap(); self.output.push_str(&fmt_rem()); self.output.push_str(s); self.output.push('\n'); @@ -126,12 +129,14 @@ impl DiffBuilder { let split = self.edit.split('\n').enumerate(); for (i, s) in split { - self.output.push_str(&format!( + write!( + self.output, "{:width$}{} ", self.edit_line + i, colors::gray(" |"), width = self.line_number_width - )); + ) + .unwrap(); self.output.push_str(&fmt_add()); self.output.push_str(s); self.output.push('\n'); diff --git a/cli/fmt_errors.rs b/cli/fmt_errors.rs index 37a58364af..759850cecc 100644 --- a/cli/fmt_errors.rs +++ b/cli/fmt_errors.rs @@ -7,6 +7,7 @@ use crate::colors::yellow; use deno_core::error::format_file_name; use deno_core::error::JsError; use deno_core::error::JsStackFrame; +use std::fmt::Write as _; // Keep in sync with `/core/error.js`. pub fn format_location(frame: &JsStackFrame) -> String { @@ -29,9 +30,9 @@ pub fn format_location(frame: &JsStackFrame) -> String { result += &cyan("").to_string(); } if let Some(line_number) = frame.line_number { - result += &format!("{}{}", ":", yellow(&line_number.to_string())); + write!(result, ":{}", yellow(&line_number.to_string())).unwrap(); if let Some(column_number) = frame.column_number { - result += &format!("{}{}", ":", yellow(&column_number.to_string())); + write!(result, ":{}", yellow(&column_number.to_string())).unwrap(); } } result @@ -61,18 +62,18 @@ fn format_frame(frame: &JsStackFrame) -> String { if let Some(function_name) = &frame.function_name { if let Some(type_name) = &frame.type_name { if !function_name.starts_with(type_name) { - formatted_method += &format!("{}.", type_name); + write!(formatted_method, "{}.", type_name).unwrap(); } } formatted_method += function_name; if let Some(method_name) = &frame.method_name { if !function_name.ends_with(method_name) { - formatted_method += &format!(" [as {}]", method_name); + write!(formatted_method, " [as {}]", method_name).unwrap(); } } } else { if let Some(type_name) = &frame.type_name { - formatted_method += &format!("{}.", type_name); + write!(formatted_method, "{}.", type_name).unwrap(); } if let Some(method_name) = &frame.method_name { formatted_method += method_name @@ -84,7 +85,7 @@ fn format_frame(frame: &JsStackFrame) -> String { } else if frame.is_constructor { result += "new "; if let Some(function_name) = &frame.function_name { - result += &italic_bold(&function_name).to_string(); + write!(result, "{}", italic_bold(&function_name)).unwrap(); } else { result += &cyan("").to_string(); } @@ -94,7 +95,7 @@ fn format_frame(frame: &JsStackFrame) -> String { result += &format_location(frame); return result; } - result += &format!(" ({})", format_location(frame)); + write!(result, " ({})", format_location(frame)).unwrap(); result } @@ -156,7 +157,7 @@ fn format_js_error_inner(js_error: &JsError, is_child: bool) -> String { for aggregated_error in aggregated { let error_string = format_js_error_inner(aggregated_error, true); for line in error_string.trim_start_matches("Uncaught ").lines() { - s.push_str(&format!("\n {}", line)); + write!(s, "\n {}", line).unwrap(); } } } @@ -174,14 +175,16 @@ fn format_js_error_inner(js_error: &JsError, is_child: bool) -> String { 0, )); for frame in &js_error.frames { - s.push_str(&format!("\n at {}", format_frame(frame))); + write!(s, "\n at {}", format_frame(frame)).unwrap(); } if let Some(cause) = &js_error.cause { let error_string = format_js_error_inner(cause, true); - s.push_str(&format!( + write!( + s, "\nCaused by: {}", error_string.trim_start_matches("Uncaught ") - )); + ) + .unwrap(); } s } diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index 909d17bfe3..1e9da54d6d 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -13,6 +13,7 @@ use log::error; use log::warn; use serde_json::from_value; use std::env; +use std::fmt::Write as _; use std::path::PathBuf; use std::sync::Arc; use tokio::fs; @@ -2878,7 +2879,8 @@ impl Inner { let measures = self.performance.to_vec(); let workspace_settings = self.config.get_workspace_settings(); - contents.push_str(&format!( + write!( + contents, r#"# Deno Language Server Status ## Workspace Settings @@ -2914,16 +2916,19 @@ impl Inner { .map(|m| m.to_string()) .collect::>() .join("\n - ") - )); + ) + .unwrap(); contents .push_str("\n## Performance\n\n|Name|Duration|Count|\n|---|---|---|\n"); let mut averages = self.performance.averages(); averages.sort(); for average in averages { - contents.push_str(&format!( + writeln!( + contents, "|{}|{}ms|{}|\n", average.name, average.average_duration, average.count - )); + ) + .unwrap(); } Some(contents) } else { diff --git a/cli/lsp/path_to_regex.rs b/cli/lsp/path_to_regex.rs index b1929c9b93..937136ce31 100644 --- a/cli/lsp/path_to_regex.rs +++ b/cli/lsp/path_to_regex.rs @@ -33,6 +33,7 @@ use once_cell::sync::Lazy; use regex::Regex; use std::collections::HashMap; use std::fmt; +use std::fmt::Write as _; use std::iter::Peekable; static ESCAPE_STRING_RE: Lazy = @@ -268,9 +269,9 @@ impl StringOrVec { let mut s = String::new(); for (i, segment) in v.iter().enumerate() { if omit_initial_prefix && i == 0 { - s.push_str(&format!("{}{}", segment, suffix)); + write!(s, "{}{}", segment, suffix).unwrap(); } else { - s.push_str(&format!("{}{}{}", prefix, segment, suffix)); + write!(s, "{}{}{}", prefix, segment, suffix).unwrap(); } } s @@ -618,10 +619,10 @@ pub fn tokens_to_regex( if end { if !strict { - route.push_str(&format!(r"{}?", delimiter)); + write!(route, r"{}?", delimiter).unwrap(); } if has_ends_with { - route.push_str(&format!(r"(?={})", ends_with)); + write!(route, r"(?={})", ends_with).unwrap(); } else { route.push('$'); } @@ -639,11 +640,11 @@ pub fn tokens_to_regex( }; if !strict { - route.push_str(&format!(r"(?:{}(?={}))?", delimiter, ends_with)); + write!(route, r"(?:{}(?={}))?", delimiter, ends_with).unwrap(); } if !is_end_deliminated { - route.push_str(&format!(r"(?={}|{})", delimiter, ends_with)); + write!(route, r"(?={}|{})", delimiter, ends_with).unwrap(); } } @@ -753,7 +754,7 @@ impl Compiler { } } } - path.push_str(&format!("{}{}{}", prefix, segment, suffix)); + write!(path, "{}{}{}", prefix, segment, suffix).unwrap(); } } } @@ -772,7 +773,7 @@ impl Compiler { } let prefix = k.prefix.clone().unwrap_or_default(); let suffix = k.suffix.clone().unwrap_or_default(); - path.push_str(&format!("{}{}{}", prefix, s, suffix)); + write!(path, "{}{}{}", prefix, s, suffix).unwrap(); } None => { if !optional { diff --git a/cli/tests/integration/vendor_tests.rs b/cli/tests/integration/vendor_tests.rs index 9b8211cd16..54809dacf6 100644 --- a/cli/tests/integration/vendor_tests.rs +++ b/cli/tests/integration/vendor_tests.rs @@ -3,6 +3,7 @@ use deno_core::serde_json; use deno_core::serde_json::json; use pretty_assertions::assert_eq; +use std::fmt::Write as _; use std::path::PathBuf; use std::process::Stdio; use test_util as util; @@ -530,20 +531,19 @@ fn update_existing_config_test() { fn success_text(module_count: &str, dir: &str, has_import_map: bool) -> String { let mut text = format!("Vendored {} into {} directory.", module_count, dir); if has_import_map { - text.push_str(& - format!( - concat!( - "\n\nTo use vendored modules, specify the `--import-map {}import_map.json` flag when ", - r#"invoking Deno subcommands or add an `"importMap": ""` "#, - "entry to a deno.json file.", - ), - if dir != "vendor/" { - format!("{}{}", dir.trim_end_matches('/'), if cfg!(windows) { '\\' } else {'/'}) - } else { - dir.to_string() - } - ) + let f = format!( + concat!( + "\n\nTo use vendored modules, specify the `--import-map {}import_map.json` flag when ", + r#"invoking Deno subcommands or add an `"importMap": ""` "#, + "entry to a deno.json file.", + ), + if dir != "vendor/" { + format!("{}{}", dir.trim_end_matches('/'), if cfg!(windows) { '\\' } else {'/'}) + } else { + dir.to_string() + } ); + write!(text, "{}", f).unwrap(); } text } diff --git a/cli/tools/test.rs b/cli/tools/test.rs index ab6ded1b0a..4bebe6feea 100644 --- a/cli/tools/test.rs +++ b/cli/tools/test.rs @@ -53,6 +53,7 @@ use serde::Deserialize; use std::collections::BTreeMap; use std::collections::HashMap; use std::collections::HashSet; +use std::fmt::Write as _; use std::io::Read; use std::io::Write; use std::num::NonZeroUsize; @@ -618,27 +619,33 @@ impl TestReporter for PrettyTestReporter { let mut summary_result = String::new(); - summary_result.push_str(&format!( + write!( + summary_result, "{} passed{} | {} failed{}", summary.passed, get_steps_text(summary.passed_steps), summary.failed, get_steps_text(summary.failed_steps + summary.pending_steps), - )); + ) + .unwrap(); let ignored_steps = get_steps_text(summary.ignored_steps); if summary.ignored > 0 || !ignored_steps.is_empty() { - summary_result - .push_str(&format!(" | {} ignored{}", summary.ignored, ignored_steps)) - }; + write!( + summary_result, + " | {} ignored{}", + summary.ignored, ignored_steps + ) + .unwrap() + } if summary.measured > 0 { - summary_result.push_str(&format!(" | {} measured", summary.measured,)) - }; + write!(summary_result, " | {} measured", summary.measured,).unwrap(); + } if summary.filtered_out > 0 { - summary_result - .push_str(&format!(" | {} filtered out", summary.filtered_out,)) + write!(summary_result, " | {} filtered out", summary.filtered_out) + .unwrap() }; println!( @@ -891,7 +898,7 @@ fn extract_files_from_regex_blocks( let mut file_source = String::new(); for line in lines_regex.captures_iter(text) { let text = line.get(1).unwrap(); - file_source.push_str(&format!("{}\n", text.as_str())); + writeln!(file_source, "{}", text.as_str()).unwrap(); } let file_specifier = deno_core::resolve_url_or_path(&format!( diff --git a/cli/tools/vendor/analyze.rs b/cli/tools/vendor/analyze.rs index 6a1f53aae0..0aa7010c73 100644 --- a/cli/tools/vendor/analyze.rs +++ b/cli/tools/vendor/analyze.rs @@ -25,7 +25,7 @@ struct DefaultExportFinder { has_default_export: bool, } -impl<'a> Visit for DefaultExportFinder { +impl Visit for DefaultExportFinder { noop_visit_type!(); fn visit_export_default_decl(&mut self, _: &ExportDefaultDecl) { diff --git a/cli/tools/vendor/build.rs b/cli/tools/vendor/build.rs index ecb7db717c..341ef86841 100644 --- a/cli/tools/vendor/build.rs +++ b/cli/tools/vendor/build.rs @@ -1,5 +1,6 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. +use std::fmt::Write as _; use std::path::Path; use std::path::PathBuf; @@ -164,10 +165,14 @@ fn build_proxy_module_source( module: &Module, proxied_module: &ProxiedModule, ) -> String { - let mut text = format!( - "// @deno-types=\"{}\"\n", + let mut text = String::new(); + writeln!( + text, + "// @deno-types=\"{}\"", proxied_module.declaration_specifier - ); + ) + .unwrap(); + let relative_specifier = format!( "./{}", proxied_module @@ -179,15 +184,17 @@ fn build_proxy_module_source( // for simplicity, always include the `export *` statement as it won't error // even when the module does not contain a named export - text.push_str(&format!("export * from \"{}\";\n", relative_specifier)); + writeln!(text, "export * from \"{}\";", relative_specifier).unwrap(); // add a default export if one exists in the module if let Some(parsed_source) = module.maybe_parsed_source.as_ref() { if has_default_export(parsed_source) { - text.push_str(&format!( - "export {{ default }} from \"{}\";\n", + writeln!( + text, + "export {{ default }} from \"{}\";", relative_specifier - )); + ) + .unwrap(); } } diff --git a/core/runtime.rs b/core/runtime.rs index 98c95b39ff..4a2f59f488 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -1012,7 +1012,8 @@ Pending dynamic modules:\n".to_string(); let module_info = module_map .get_info_by_id(&pending_evaluate.module_id) .unwrap(); - msg.push_str(&format!("- {}", module_info.name.as_str())); + msg.push_str("- "); + msg.push_str(module_info.name.as_str()); } return Poll::Ready(Err(generic_error(msg))); } else { diff --git a/ext/ffi/lib.rs b/ext/ffi/lib.rs index ca02aca5bb..17ebf772b8 100644 --- a/ext/ffi/lib.rs +++ b/ext/ffi/lib.rs @@ -573,9 +573,8 @@ where // By default, Err returned by this function does not tell // which symbol wasn't exported. So we'll modify the error // message to include the name of symbol. - // - // SAFETY: The obtained T symbol is the size of a pointer. let fn_ptr = + // SAFETY: The obtained T symbol is the size of a pointer. match unsafe { resource.lib.symbol::<*const c_void>(symbol) } { Ok(value) => Ok(value), Err(err) => Err(generic_error(format!( diff --git a/ext/net/ops.rs b/ext/net/ops.rs index 3522a919ce..87bfc32722 100644 --- a/ext/net/ops.rs +++ b/ext/net/ops.rs @@ -754,13 +754,14 @@ fn rdata_to_return_record( let mut s = String::new(); if let Some(name) = name { - s.push_str(&format!("{}", name)); + s.push_str(&name.to_string()); } else if name.is_none() && key_values.is_empty() { s.push(';'); } for key_value in key_values { - s.push_str(&format!("; {}", key_value)); + s.push_str("; "); + s.push_str(&key_value.to_string()); } s diff --git a/ext/webgpu/src/bundle.rs b/ext/webgpu/src/bundle.rs index 0c1f822025..1f5e4da7b8 100644 --- a/ext/webgpu/src/bundle.rs +++ b/ext/webgpu/src/bundle.rs @@ -152,9 +152,9 @@ pub fn op_webgpu_render_bundle_encoder_set_bind_group( // Align the data assert!(args.dynamic_offsets_data.len() % std::mem::size_of::() == 0); - // SAFETY: A u8 to u32 cast is safe because we asserted that the length is a - // multiple of 4. let (prefix, dynamic_offsets_data, suffix) = + // SAFETY: A u8 to u32 cast is safe because we asserted that the length is a + // multiple of 4. unsafe { args.dynamic_offsets_data.align_to::() }; assert!(prefix.is_empty()); assert!(suffix.is_empty()); diff --git a/ext/webgpu/src/compute_pass.rs b/ext/webgpu/src/compute_pass.rs index 9f6394f7ba..5e27583f4b 100644 --- a/ext/webgpu/src/compute_pass.rs +++ b/ext/webgpu/src/compute_pass.rs @@ -241,9 +241,9 @@ pub fn op_webgpu_compute_pass_set_bind_group( // Align the data assert!(args.dynamic_offsets_data_start % std::mem::size_of::() == 0); - // SAFETY: A u8 to u32 cast is safe because we asserted that the length is a - // multiple of 4. let (prefix, dynamic_offsets_data, suffix) = + // SAFETY: A u8 to u32 cast is safe because we asserted that the length is a + // multiple of 4. unsafe { args.dynamic_offsets_data.align_to::() }; assert!(prefix.is_empty()); assert!(suffix.is_empty()); diff --git a/ext/webgpu/src/render_pass.rs b/ext/webgpu/src/render_pass.rs index 16398359fc..5d2cd6e5a1 100644 --- a/ext/webgpu/src/render_pass.rs +++ b/ext/webgpu/src/render_pass.rs @@ -305,9 +305,9 @@ pub fn op_webgpu_render_pass_set_bind_group( // Align the data assert!(args.dynamic_offsets_data_start % std::mem::size_of::() == 0); - // SAFETY: A u8 to u32 cast is safe because we asserted that the length is a - // multiple of 4. let (prefix, dynamic_offsets_data, suffix) = + // SAFETY: A u8 to u32 cast is safe because we asserted that the length is a + // multiple of 4. unsafe { args.dynamic_offsets_data.align_to::() }; assert!(prefix.is_empty()); assert!(suffix.is_empty()); diff --git a/rust-toolchain.toml b/rust-toolchain.toml index e2609326f7..7255222461 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] -channel = "1.61.0" +channel = "1.62.0" components = ["rustfmt", "clippy"] diff --git a/serde_v8/de.rs b/serde_v8/de.rs index 7b825f990c..08b28c0f5c 100644 --- a/serde_v8/de.rs +++ b/serde_v8/de.rs @@ -580,7 +580,7 @@ struct EnumAccess<'a, 'b, 's> { // p1: std::marker::PhantomData<&'x ()>, } -impl<'de, 'a, 'b, 's, 'x> de::EnumAccess<'de> for EnumAccess<'a, 'b, 's> { +impl<'de, 'a, 'b, 's> de::EnumAccess<'de> for EnumAccess<'a, 'b, 's> { type Error = Error; type Variant = VariantDeserializer<'a, 'b, 's>; diff --git a/serde_v8/magic/bytestring.rs b/serde_v8/magic/bytestring.rs index 3efb56f6aa..c7c1b9de83 100644 --- a/serde_v8/magic/bytestring.rs +++ b/serde_v8/magic/bytestring.rs @@ -51,9 +51,9 @@ impl FromV8 for ByteString { } let len = v8str.length(); let mut buffer = SmallVec::with_capacity(len); + #[allow(clippy::uninit_vec)] // SAFETY: we set length == capacity (see previous line), // before immediately writing into that buffer and sanity check with an assert - #[allow(clippy::uninit_vec)] unsafe { buffer.set_len(len); let written = v8str.write_one_byte( diff --git a/serde_v8/magic/rawbytes.rs b/serde_v8/magic/rawbytes.rs index 2189ebfc33..4e41d313a5 100644 --- a/serde_v8/magic/rawbytes.rs +++ b/serde_v8/magic/rawbytes.rs @@ -87,11 +87,11 @@ mod tests { #[test] fn bytes_layout() { - // SAFETY: ensuring layout is the same let u1: [usize; 4] = + // SAFETY: ensuring layout is the same unsafe { mem::transmute(from_static(HELLO.as_bytes())) }; - // SAFETY: ensuring layout is the same let u2: [usize; 4] = + // SAFETY: ensuring layout is the same unsafe { mem::transmute(bytes::Bytes::from_static(HELLO.as_bytes())) }; assert_eq!(u1[..3], u2[..3]); // Struct bytes are equal besides Vtables } diff --git a/serde_v8/magic/u16string.rs b/serde_v8/magic/u16string.rs index e304ea1879..1e36879a4c 100644 --- a/serde_v8/magic/u16string.rs +++ b/serde_v8/magic/u16string.rs @@ -34,9 +34,9 @@ impl FromV8 for U16String { .map_err(|_| Error::ExpectedString)?; let len = v8str.length(); let mut buffer = Vec::with_capacity(len); + #[allow(clippy::uninit_vec)] // SAFETY: we set length == capacity (see previous line), // before immediately writing into that buffer and sanity check with an assert - #[allow(clippy::uninit_vec)] unsafe { buffer.set_len(len); let written = v8str.write( diff --git a/serde_v8/magic/v8slice.rs b/serde_v8/magic/v8slice.rs index 452c857a33..67255fc537 100644 --- a/serde_v8/magic/v8slice.rs +++ b/serde_v8/magic/v8slice.rs @@ -50,6 +50,7 @@ impl V8Slice { } fn as_slice_mut(&mut self) -> &mut [u8] { + #[allow(clippy::cast_ref_to_mut)] // SAFETY: v8::SharedRef is similar to Arc<[u8]>, // it points to a fixed continuous slice of bytes on the heap. // It's safe-ish to mutate concurrently because it can not be @@ -59,7 +60,6 @@ impl V8Slice { // concurrent mutation is simply an accepted fact of life. // And in practice V8Slices also do not have overallping read/write phases. // TLDR: permissive interior mutability on slices of bytes is "fine" - #[allow(clippy::cast_ref_to_mut)] unsafe { &mut *(&self.store[self.range.clone()] as *const _ as *mut [u8]) }