Skip to content

Commit bb6bbfa

Browse files
committed
Avoid naming variables str
This renames variables named `str` to other names, to make sure `str` always refers to a type. It's confusing to read code where `str` (or another standard type name) is used as an identifier. It also produces misleading syntax highlighting.
1 parent fb546ee commit bb6bbfa

File tree

10 files changed

+34
-34
lines changed

10 files changed

+34
-34
lines changed

compiler/rustc_borrowck/src/region_infer/values.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -544,12 +544,12 @@ fn pretty_print_region_elements(elements: impl IntoIterator<Item = RegionElement
544544

545545
return result;
546546

547-
fn push_location_range(str: &mut String, location1: Location, location2: Location) {
547+
fn push_location_range(s: &mut String, location1: Location, location2: Location) {
548548
if location1 == location2 {
549-
str.push_str(&format!("{location1:?}"));
549+
s.push_str(&format!("{location1:?}"));
550550
} else {
551551
assert_eq!(location1.block, location2.block);
552-
str.push_str(&format!(
552+
s.push_str(&format!(
553553
"{:?}[{}..={}]",
554554
location1.block, location1.statement_index, location2.statement_index
555555
));

compiler/rustc_const_eval/src/interpret/operand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
704704
pub fn read_str(&self, mplace: &MPlaceTy<'tcx, M::Provenance>) -> InterpResult<'tcx, &str> {
705705
let len = mplace.len(self)?;
706706
let bytes = self.read_bytes_ptr_strip_provenance(mplace.ptr(), Size::from_bytes(len))?;
707-
let str = std::str::from_utf8(bytes).map_err(|err| err_ub!(InvalidStr(err)))?;
708-
interp_ok(str)
707+
let s = std::str::from_utf8(bytes).map_err(|err| err_ub!(InvalidStr(err)))?;
708+
interp_ok(s)
709709
}
710710

711711
/// Read from a local of the current frame. Convenience method for [`InterpCx::local_at_frame_to_op`].

compiler/rustc_const_eval/src/interpret/place.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1017,9 +1017,9 @@ where
10171017
/// This is allocated in immutable global memory and deduplicated.
10181018
pub fn allocate_str_dedup(
10191019
&mut self,
1020-
str: &str,
1020+
s: &str,
10211021
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
1022-
let bytes = str.as_bytes();
1022+
let bytes = s.as_bytes();
10231023
let ptr = self.allocate_bytes_dedup(bytes)?;
10241024

10251025
// Create length metadata for the string.

compiler/rustc_lint/src/nonstandard_style.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,18 @@ declare_lint! {
234234
declare_lint_pass!(NonSnakeCase => [NON_SNAKE_CASE]);
235235

236236
impl NonSnakeCase {
237-
fn to_snake_case(mut str: &str) -> String {
237+
fn to_snake_case(mut name: &str) -> String {
238238
let mut words = vec![];
239239
// Preserve leading underscores
240-
str = str.trim_start_matches(|c: char| {
240+
name = name.trim_start_matches(|c: char| {
241241
if c == '_' {
242242
words.push(String::new());
243243
true
244244
} else {
245245
false
246246
}
247247
});
248-
for s in str.split('_') {
248+
for s in name.split('_') {
249249
let mut last_upper = false;
250250
let mut buf = String::new();
251251
if s.is_empty() {

compiler/rustc_log/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ pub fn init_logger(cfg: LoggerConfig) -> Result<(), Error> {
130130

131131
let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer);
132132
match cfg.backtrace {
133-
Ok(str) => {
133+
Ok(backtrace_target) => {
134134
let fmt_layer = tracing_subscriber::fmt::layer()
135135
.with_writer(io::stderr)
136136
.without_time()
137-
.event_format(BacktraceFormatter { backtrace_target: str });
137+
.event_format(BacktraceFormatter { backtrace_target });
138138
let subscriber = subscriber.with(fmt_layer);
139139
tracing::subscriber::set_global_default(subscriber).unwrap();
140140
}

compiler/rustc_macros/src/symbols.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ impl Entries {
156156
Entries { map: HashMap::with_capacity(capacity) }
157157
}
158158

159-
fn insert(&mut self, span: Span, str: &str, errors: &mut Errors) -> u32 {
160-
if let Some(prev) = self.map.get(str) {
161-
errors.error(span, format!("Symbol `{str}` is duplicated"));
159+
fn insert(&mut self, span: Span, s: &str, errors: &mut Errors) -> u32 {
160+
if let Some(prev) = self.map.get(s) {
161+
errors.error(span, format!("Symbol `{s}` is duplicated"));
162162
errors.error(prev.span_of_name, "location of previous definition".to_string());
163163
prev.idx
164164
} else {
165165
let idx = self.len();
166-
self.map.insert(str.to_string(), Preinterned { idx, span_of_name: span });
166+
self.map.insert(s.to_string(), Preinterned { idx, span_of_name: span });
167167
idx
168168
}
169169
}
@@ -192,14 +192,14 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
192192
let mut entries = Entries::with_capacity(input.keywords.len() + input.symbols.len() + 10);
193193
let mut prev_key: Option<(Span, String)> = None;
194194

195-
let mut check_order = |span: Span, str: &str, errors: &mut Errors| {
195+
let mut check_order = |span: Span, s: &str, errors: &mut Errors| {
196196
if let Some((prev_span, ref prev_str)) = prev_key {
197-
if str < prev_str {
198-
errors.error(span, format!("Symbol `{str}` must precede `{prev_str}`"));
197+
if s < prev_str {
198+
errors.error(span, format!("Symbol `{s}` must precede `{prev_str}`"));
199199
errors.error(prev_span, format!("location of previous symbol `{prev_str}`"));
200200
}
201201
}
202-
prev_key = Some((span, str.to_string()));
202+
prev_key = Some((span, s.to_string()));
203203
};
204204

205205
// Generate the listed keywords.

library/std/src/process.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1283,13 +1283,13 @@ impl fmt::Debug for Output {
12831283
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
12841284
let stdout_utf8 = str::from_utf8(&self.stdout);
12851285
let stdout_debug: &dyn fmt::Debug = match stdout_utf8 {
1286-
Ok(ref str) => str,
1286+
Ok(ref s) => s,
12871287
Err(_) => &self.stdout,
12881288
};
12891289

12901290
let stderr_utf8 = str::from_utf8(&self.stderr);
12911291
let stderr_debug: &dyn fmt::Debug = match stderr_utf8 {
1292-
Ok(ref str) => str,
1292+
Ok(ref s) => s,
12931293
Err(_) => &self.stderr,
12941294
};
12951295

library/std/src/sys/pal/windows/process.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ impl AsRef<OsStr> for EnvKey {
142142
}
143143
}
144144

145-
pub(crate) fn ensure_no_nuls<T: AsRef<OsStr>>(str: T) -> io::Result<T> {
146-
if str.as_ref().encode_wide().any(|b| b == 0) {
145+
pub(crate) fn ensure_no_nuls<T: AsRef<OsStr>>(s: T) -> io::Result<T> {
146+
if s.as_ref().encode_wide().any(|b| b == 0) {
147147
Err(io::const_error!(ErrorKind::InvalidInput, "nul byte found in provided data"))
148148
} else {
149-
Ok(str)
149+
Ok(s)
150150
}
151151
}
152152

library/std/src/sys_common/wtf8.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ impl Wtf8Buf {
204204
///
205205
/// Since WTF-8 is a superset of UTF-8, this always succeeds.
206206
#[inline]
207-
pub fn from_str(str: &str) -> Wtf8Buf {
208-
Wtf8Buf { bytes: <[_]>::to_vec(str.as_bytes()), is_known_utf8: true }
207+
pub fn from_str(s: &str) -> Wtf8Buf {
208+
Wtf8Buf { bytes: <[_]>::to_vec(s.as_bytes()), is_known_utf8: true }
209209
}
210210

211211
pub fn clear(&mut self) {

src/tools/compiletest/src/compute_diff.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Misma
3131

3232
for result in diff::lines(expected, actual) {
3333
match result {
34-
diff::Result::Left(str) => {
34+
diff::Result::Left(s) => {
3535
if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
3636
results.push(mismatch);
3737
mismatch = Mismatch::new(line_number - context_queue.len() as u32);
@@ -41,11 +41,11 @@ pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Misma
4141
mismatch.lines.push(DiffLine::Context(line.to_owned()));
4242
}
4343

44-
mismatch.lines.push(DiffLine::Expected(str.to_owned()));
44+
mismatch.lines.push(DiffLine::Expected(s.to_owned()));
4545
line_number += 1;
4646
lines_since_mismatch = 0;
4747
}
48-
diff::Result::Right(str) => {
48+
diff::Result::Right(s) => {
4949
if lines_since_mismatch >= context_size && lines_since_mismatch > 0 {
5050
results.push(mismatch);
5151
mismatch = Mismatch::new(line_number - context_queue.len() as u32);
@@ -55,18 +55,18 @@ pub fn make_diff(expected: &str, actual: &str, context_size: usize) -> Vec<Misma
5555
mismatch.lines.push(DiffLine::Context(line.to_owned()));
5656
}
5757

58-
mismatch.lines.push(DiffLine::Resulting(str.to_owned()));
58+
mismatch.lines.push(DiffLine::Resulting(s.to_owned()));
5959
lines_since_mismatch = 0;
6060
}
61-
diff::Result::Both(str, _) => {
61+
diff::Result::Both(s, _) => {
6262
if context_queue.len() >= context_size {
6363
let _ = context_queue.pop_front();
6464
}
6565

6666
if lines_since_mismatch < context_size {
67-
mismatch.lines.push(DiffLine::Context(str.to_owned()));
67+
mismatch.lines.push(DiffLine::Context(s.to_owned()));
6868
} else if context_size > 0 {
69-
context_queue.push_back(str);
69+
context_queue.push_back(s);
7070
}
7171

7272
line_number += 1;

0 commit comments

Comments
 (0)