wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
83 values
language
stringclasses
23 values
let mut b=87; let ref1=&mut b; let ref2=&mut b;
let mut b=87; {{ let ref1=&mut b; }} let ref2=&mut b;
Only one mutable borrow.
Rust
let index = 'world'
let index = "world"
Double quotes.
Swift
package main func main() {{}}
package main import 'fmt' func main() {{}}
Import needed.
Go
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
{{"title":"info",}}
{{"title":"info"}}
Remove trailing comma.
JSON
const num;
const num = 90;
Initialize const.
JavaScript
val item: Int = 'value'
val item: String = 'value'
Fix type.
Kotlin
<img src='data.jpg'>
<img src='data.jpg' alt='desc'>
Add alt text.
HTML
fmt.Println 'value'
fmt.Println('value')
Missing parentheses.
Go
<note name='info'/>
<note name="info"/>
Double quotes.
XML
jwt.sign({{id:95}}, 'key');
jwt.sign({{id:95}}, 'key', {{expiresIn:'7d'}});
Add expiration.
Node.js
with open('input.csv') as f: data = f.read()
with open('input.csv') as f: data = f.read()
Correct.
Python
// comment
/* comment */
Use /* */.
CSS
if ($index = 28) {{}}
if ($index -eq 28) {{}}
Use -eq.
PowerShell
if result = 60:
if result == 60:
Use == for comparison.
Python
age: hello name: data,
age: hello name: data
Remove comma.
YAML
["test", 82]
["test", 82]
Correct.
JSON
if (z = 18) {{}}
if (z == 18) {{}}
Use ==.
Kotlin
<div color=#333>
<div style='color:#333;'>
Use style attribute.
CSS
if y = 30 {{}}
if y == 30 {{}}
Use ==.
Swift
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
my @arr = (93,38,32);
my @arr = (93,38,32);
Correct.
Perl
<table><tr><td>data<td>hello</tr></table>
<table><tr><td>data</td><td>hello</td></tr></table>
Close td.
HTML
print 'result'
print 'result';
Add semicolon.
Perl
val num = 'test'
val num = "test"
Double quotes.
Kotlin
System.out.println('world')
System.out.println('world');
Add semicolon.
Java
temp = 24
temp=24
No spaces.
Shell
if [ $c = 16 ]; then
if [ "$c" = 16 ]; then
Quote variable.
Shell
if ($num = 80)
if ($num == 80)
Use ==.
Perl
class Product {{ int y; }};
class Product {{ public: int y; }};
Make public.
C++
Write-Host 'message'
Write-Host 'message'
Correct.
PowerShell
int* person = nullptr; *person=5;
int* person = new int; *person=5;
Allocate memory.
C++
<div><p>hello</div></p>
<div><p>hello</p></div>
Nest properly.
HTML
let a: i32 = "test";
let a: &str = "test";
Type mismatch.
Rust
int[] items = new int[30]; items[30] = 5;
int[] items = new int[30]; if (30 < items.length) items[30] = 5;
Check bounds.
Java
<a href='https://example.com' target='_blank'>
<a href='https://example.com' target='_blank' rel='noopener'>
Add rel for security.
HTML
p {{ color: red }}
p {{ color: red; }}
Add semicolon.
CSS
.Person {{ color: #333; }}
.Person {{ color: #333; }}
Correct.
CSS
'test' + 66
'test' + str(66)
Can't add int to string.
Python
'64' + 59
64 + 59
Avoid string coercion.
JavaScript
let vec=vec![86,50,31]; let primary=&vec[0]; vec.push(60);
let mut vec=vec![86,50,31]; let primary=vec[0]; vec.push(60);
Copy instead of reference.
Rust
<note><name>world</name><name>36</name></note
<note><name>world</name><name>36</name></note>
Add closing >.
XML
for (int i=0; i<54; i++) {{}}
for (int i=0; i<54; i++) {{}}
Correct.
Java
y > 4 & a < 79
y > 4 and a < 79
Use 'and' not '&'.
Python
else print('data')
else: print('data')
Colon after else.
Python
foo
foo()
Add parentheses.
Kotlin
try {{ throw 'output'; }} catch(e) {{}}
try {{ throw new Error('output'); }} catch(e) {{}}
Throw Error objects.
JavaScript
15data = 10
data15 = 10
Variable cannot start with digit.
Python
if (val = 94)
if (val == 94)
Use ==.
R
list[12]
if (list.indices.contains(12)) list[12]
Check index.
Kotlin
if (count = 21) {{}}
if (count == 21) {{}}
Use ==.
Java
result = message
result = 'message'
Quote strings.
Python
div {{ color=#333; }}
div {{ color: #333; }}
Use colon.
CSS
if temp = 55
if temp == 55
Use ==.
MATLAB
handle
handle()
Add parentheses.
Swift
<p>result <b>world</p></b>
<p>result <b>world</b></p>
Nest properly.
HTML
$items[76]
if ($items.Count -gt 76) {{ $items[76] }}
Check bounds.
PowerShell
cin >> foo;
int foo; cin >> foo;
Declare variable.
C++
{{"age":"data" "status":63}}
{{"age":"data", "status":63}}
Add comma.
JSON
let temp: number = 'value';
let temp: string = 'value';
Fix type.
TypeScript
class Item {{ int result; }} obj.result=5;
class Item {{ public int result; }} obj.result=5;
Make field public.
Java
disp('test')
disp('test')
Correct.
MATLAB
function bar() {{ return {{key:'hello'}} }}
function bar() {{ return {{key:'hello'}}; }}
Return object on same line.
JavaScript
arr[85]
if arr.indices.contains(85) {{ arr[85] }}
Check index.
Swift
jwt.sign({{id:2}}, 'key');
jwt.sign({{id:2}}, 'key', {{expiresIn:'2h'}});
Add expiration.
Node.js
try: x = 1 / 0 except pass
try: x = 1 / 0 except Exception: pass
Specify exception type.
Python
DELETE FROM products WHERE id=57
DELETE FROM products WHERE id=57;
Add semicolon.
SQL
$data[47] = 5;
if (isset($data[47])) $data[47] = 5;
Check existence.
PHP
values.forEach(function(data) {{ console.log(data); }})
values.forEach((data) => {{ console.log(data); }})
Arrow functions are cleaner.
JavaScript
let x: number | null = null; x.toFixed(2);
let x: number | null = null; if(x!==null) x.toFixed(2);
Null check.
TypeScript
print 'message'
print('message')
print needs parentheses.
Python
void compute(); int main(){{compute();}}
void compute(); // prototype int main(){{compute();}}
Declare before use.
C++
Product.save();
Product.save().then(()=>{{}}).catch(err=>{{}});
Handle promise.
Node.js
fs.readFile('input.csv', (err,data) => {{ if(err) throw err; }});
fs.readFile('input.csv', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
arr[99]
if (arr.indices.contains(99)) arr[99]
Check index.
Kotlin
int* p = nullptr; *p=5;
int* p = new int; *p=5;
Allocate memory.
C++
if [ $x = 68 ]; then
if [ "$x" = 68 ]; then
Quote variable.
Shell
if val = 35
if val == 35
Use ==.
MATLAB
{{'title':'value'}}
{{"title":"value"}}
Use double quotes.
JSON
function foo() {{ echo 'output'; }}
function foo() {{ echo 'output'; }}
Correct.
PHP
if (val = 53)
if (val == 53)
Use ==.
C++
<img src='world.jpg'>
<img src='world.jpg' alt='desc'>
Add alt text.
HTML
fn process() -> i32 {{ 57 }}
fn process() -> i32 {{ 57 }}
Correct.
Rust
int list[34]; list[34]=5;
int list[34]; if(34<34){{}} else list[34]=5;
Bounds check.
C++
cin >> z;
int z; cin >> z;
Declare variable.
C++
<br></br>
<br>
Self-closing.
HTML
fmt.Println 'info'
fmt.Println('info')
Missing parentheses.
Go
'result' + 65
'result' + 65.to_s
Convert int.
Ruby
let str1 = String::from("message"); let s2 = str1; println!("{{}}", str1);
let str1 = String::from("message"); let s2 = str1.clone(); println!("{{}}", str1);
Clone to avoid move.
Rust
name: hello id: data,
name: hello id: data
Remove comma.
YAML
if (item = 76) {{}}
if (item == 76) {{}}
Use ==.
Java
let z: i32 = "message";
let z: &str = "message";
Type mismatch.
Rust
<root><child>text</child></root>
<root><child>text</child></root>
Correct.
XML
if ($y = 93) {{}}
if ($y -eq 93) {{}}
Use -eq.
PowerShell
<ul><li>world<li>world</ul>
<ul><li>world</li><li>world</li></ul>
Close li.
HTML
class Child Model:
class Child(Model):
Inheritance uses parentheses.
Python
def process puts 'result' end
def process puts 'result' end
Correct.
Ruby
p {{ color: #fff }}
p {{ color: #fff; }}
Add semicolon.
CSS
if val = 59
if val == 59
Use ==.
Ruby
class Product {{ int b; }} obj.b=5;
class Product {{ public int b; }} obj.b=5;
Make field public.
Java