use proper name instead of magic number · model-checking/verify-rust-std@8a6d10d (original) (raw)

2 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -98,7 +98,7 @@ fn fmt_thousands_sep(mut n: f64, sep: char) -> String {
98 98 (0, true) => write!(output, "{:06.2}", n / base as f64).unwrap(),
99 99 (0, false) => write!(output, "{:.2}", n / base as f64).unwrap(),
100 100 (_, true) => write!(output, "{:03}", n as usize / base).unwrap(),
101 - _ => write!(output, "{}", n as usize / base).unwrap()
101 + _ => write!(output, "{}", n as usize / base).unwrap(),
102 102 }
103 103 if pow != 0 {
104 104 output.push(sep);
Original file line number Diff line number Diff line change
@@ -22,6 +22,8 @@ type WORD = u16;
22 22 type DWORD = u32;
23 23 type BOOL = i32;
24 24 type HANDLE = *mut u8;
25 +// https://docs.microsoft.com/en-us/windows/console/getstdhandle
26 +const STD_OUTPUT_HANDLE: DWORD = -11 as _;
25 27
26 28 #[allow(non_snake_case)]
27 29 #[repr(C)]
@@ -99,16 +101,13 @@ impl<T: Write + Send + 'static> WinConsole {
99 101 accum |= color_to_bits(self.background) << 4;
100 102
101 103 unsafe {
102 -// Magic -11 means stdout, from
103 -// https://docs.microsoft.com/en-us/windows/console/getstdhandle
104 -//
105 104 // You may be wondering, "but what about stderr?", and the answer
106 105 // to that is that setting terminal attributes on the stdout
107 106 // handle also sets them for stderr, since they go to the same
108 107 // terminal! Admittedly, this is fragile, since stderr could be
109 108 // redirected to a different console. This is good enough for
110 109 // rustc though. See #13400.
111 -let out = GetStdHandle(-11i32 as DWORD);
110 +let out = GetStdHandle(STD_OUTPUT_HANDLE);
112 111 SetConsoleTextAttribute(out, accum);
113 112 }
114 113 }
@@ -120,9 +119,8 @@ impl<T: Write + Send + 'static> WinConsole {
120 119 let bg;
121 120 unsafe {
122 121 let mut buffer_info = MaybeUninit::<CONSOLE_SCREEN_BUFFER_INFO>::uninit();
123 -if GetConsoleScreenBufferInfo(GetStdHandle(-11i32 as DWORD), buffer_info.as_mut_ptr())
124 - != 0
125 -{
122 +let handle = GetStdHandle(STD_OUTPUT_HANDLE);
123 +if GetConsoleScreenBufferInfo(handle, buffer_info.as_mut_ptr()) != 0 {
126 124 let buffer_info = buffer_info.assume_init();
127 125 fg = bits_to_color(buffer_info.wAttributes);
128 126 bg = bits_to_color(buffer_info.wAttributes >> 4);